Smoother transitions when showing a View Controller with a UINavigationBar in Swift

Here’s the scenario. You want to display a modal view controller without a navigation bar, and then from that view controller you want to navigate to another view controller that displays a navigation bar.

First to get things set up, in the Storyboard we set up the view controller that would be displayed modally. We create a view controller, perform the Editor -> Embed in -> Navigation Controller. Then, in the viewWillAppear method, we hide the navigation bar programmatically.

It turns out that there are two ways to do this and, at least in my case, one way turned out to be better than the other. The first way I tried worked, but the animation for the transition from one view controller to the next looked a little odd.

The first way is the more obvious of the two, because you can simply just set the hidden property of the navigationBar like in the following example:

self.navigationController?.navigationBar.hidden = true

There is also another way to do the hiding and showing of the UINavigationBar, and the second way is do do it via the UINavigationController, as in the second example:

self.navigationController?.setNavigationBarHidden(true, animated: true)

There are two advantages to doing it the second way. First, because you get a method to hide and show the navigation bar, you get the additional animated parameter that you can set.

The second advantage is that when you use it in this way with the animated parameter set to true, iOS performs a slightly more smooth transition.

Your mileage may vary, so try both ways and see which you personally like best.