0

I have three controllers (FirstVC, SecondVC, ThirdVC) inside storyboad, and navigation is sequential: a user can present modally SecondVC from FirstVC. and then present modally ThirdVC from SecondVC. Now, I need to make some button that will open ThirdVC from FirstVC but will also put SecondVC on in between, so when a user will press back/Cross from ThirdVC he will be returned to SecondVC. So, I don’t need animation from FirstVC to SecondVC, just need to go SecondVC and then animate only transition to ThirdVC.

I found same question for push sague How to push two view controllers but animate transition only for the second one?. I need same behaviour with present modally.

invisible squirrel
  • 2,968
  • 3
  • 25
  • 26

2 Answers2

1

You cannot present a view controller until the view of the presenting view controller has rendered. Therefore, it's not possible to present two view controllers simultaneously where the second presentation originates from the first--the first must first render its view.

But the solution is still rather basic. If the user is going from A to B, you present B from A like normal. If the user is going from A to C, you present C from A but C is a container view controller with two children (B and C, C obviously on top). Therefore, when the user dismisses C, he is not really dismissing anything, but rather animating C out of view to show B (using the same animation as the dismiss). To the user, it looks all the same.

trndjc
  • 11,654
  • 3
  • 38
  • 51
1

Here will be the hierarchy First VC presents second VC

let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondVC") as! SecondVC
    present(vc, animated: true, completion: nil)
present(vc, animated: true, completion: nil)

second VC will add third VC as subview and make it visible

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ThirdVC") as! ThirdVC
    self.addChild(vc)
    self.view.addSubview(vc.view)
}

when dismiss third VC remove it from super view with animation and reveal second VC

UIView.animate(withDuration: 0.5, animations: { [weak self] in
        self?.view.frame = CGRect(x: 0, y: self?.view.frame.height ?? 0.0, width: self?.view.frame.width ?? 0.0, height: self?.view.frame.height ?? 0.0)
    }, completion: { [weak self] _ in
        self?.view.removeFromSuperview()
    })

dismiss second VC and reveal first VC

dismiss(animated: true, completion: nil)
Habib Ali
  • 272
  • 2
  • 13