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

前端 未结 7 1456
温柔的废话
温柔的废话 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.

    0 讨论(0)
  • 2020-12-04 18:20

    Swift Version From KimAMartinsen Solution

    guard var controllers = self.navigationController?.viewControllers else {
       return
    }
    
    guard let firstVC = self.storyboard?.instantiateViewController(withIdentifier: "Tests") as? firstViewController else { 
       return
    }
    
    guard let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "Dashboard") as? SecondViewController else {
        return
    }
    
    controllers.append(firstVC)
    controllers.append(secondVC)
    self.navigationController?.setViewControllers(controllers, animated: true)
    
    0 讨论(0)
  • 2020-12-04 18:21
    extension UINavigationController {
        open func pushViewControllers(_ inViewControllers: [UIViewController], animated: Bool) {
            var stack = self.viewControllers
            stack.append(contentsOf: inViewControllers)
            self.setViewControllers(stack, animated: animated)
        }
    }
    
    0 讨论(0)
  • 2020-12-04 18:21

    I suggest pushing your view controllers manually. The first without animation:

    [self.navigationController pushViewController:SecondVC animated:NO];
    [self.navigationController pushViewController:ThirdVC animated:YES];
    
    0 讨论(0)
  • 2020-12-04 18:22

    The solution you're looking for if you're in the firstVC:

    NSMutableArray *controllers = [self.navigationController.viewControllers mutableCopy];
    [controllers addObject:secondVc];
    [controllers addObject:thirdVC];
    [self.navigationController setViewControllers:controllers animated:YES];
    

    This will animate in the thirdVC without the secondVc becoming visible in the process. When the user press the back button, they will return to the secondVc

    0 讨论(0)
  • 2020-12-04 18:30

    Hmm perhaps just write a custom segue that doesn't do any animations an make sure the segue in the storyboard references your segue class.

    https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomSegues/CreatingCustomSegues.html

    Link above to how to create it, docs IMO are fairly self explanatory. You can then customise how you segues work and appear.

    Other options include the new protocols introduced in iOS7 assuming you don't want to support older devices.

    Watch the Apple Tech Talks 2014 video "Architecting Modern Apps part 1" they demo it on there.

    https://developer.apple.com/tech-talks/videos/

    There are many solutions to your question hopefully one of the above is helpful, let me know if it's not and I'll propose another.

    UPDATE:

    Another option would be to use a tav view controller perhaps if this fits in with your needs as you could add a navigation controller to one of the tabs to achieve this or swap tabs as needed.

    0 讨论(0)
提交回复
热议问题