Hide Navigation Bar in Specific View - Swift 3

Quentin Beau Kwint picture Quentin Beau Kwint · May 2, 2017 · Viewed 11.7k times · Source

I have NavigationController that handles navigation through my app. According to my design, the very first view should have no visible NavigationBar. All the others after, will.

In this FirstView, I'm using this so far to hide the NavBar, inside the ViewDidLoad:

self.navigationController?.isNavigationBarHidden = true

From this FirstView I can access other Views. In these other views I show the NavBar using:

self.navigationController?.isNavigationBarHidden = false

My problem is that:

  • When I navigate from a View with Visible NavBar, back to the FirstView with the Hidden NavBar, the NavBar is now visible. Basically the NavBar only hides the very first time then shows if I use the back button.

How Can I Prevent this ?

Thank you!

Answer

Eggsalad picture Eggsalad · May 2, 2017

Move that code to viewWillAppear() instead of viewDidLoad().

viewDidLoad() is only called once per instantiated view controller, whereas viewWillAppear() is called whenever the view controller is about to be presented on screen.

You can read more about the view controller lifecycle here.