How to push two view controllers but animate transition only for the second one?

前端 未结 7 1455
温柔的废话
温柔的废话 2020-12-04 17:46

I have three controllers (FirstVC, SecondVC, ThirdVC) inside storyboad, and navigation is sequential: a user can navigate from FirstVC to SecondVC, and then to ThirdVC. Now,

7条回答
  •  粉色の甜心
    2020-12-04 18:09

    To preserve the standard animation for pushing a view controller, in Swift:

    let pushVC = UIViewController()
    let backVC = UIViewController()
    
    if let navigationController = navigationController {
    
      navigationController.pushViewController(pushVC, animated: true)
    
      let stackCount = navigationController.viewControllers.count
      let addIndex = stackCount - 1
      navigationController.viewControllers.insert(backVC, atIndex: addIndex)
    
    }
    

    This displays pushVC normally and inserts backVC into the navigation stack, preserving both the animation and the history for the UINavigationController.

    You can use setViewControllers, but you'll lose the standard push animation.

提交回复
热议问题