steventnorris steventnorris - 9 months ago 75
Swift Question

Animate Replacing viewControllers for UINavigationController

I have a menu that allows users to switch between view stacks. What I'd like to do is have the UINavigationController switch from one stack to the next with the slide animation.

So, to add one view to the current stack, I know I can do this:

self.navigationController!.pushViewController(myController, animated: true)

Which will slide the view onto the stack, animated.

And I know I can do this

self.navigationController!.viewControllers = newControllersStack

But that's not animated and feels hacky as it just injects into the stack flow without any proper flow. What I want to do is this

self.navigationController!.changeViewControllers(newControllersStack, animated: true)

Is there a way to do this? I've looked into a tab bar, but I'd have to hide the tabs (using a side menu for navigation) and even then, it animates transitions between UINavigationControllers, not views inside of a single nav controller.

Answer Source

Use UINavigationController.setViewControllers(_:animated:).