How to hide a navigation bar from first ViewController in Swift?

Vettiyanakan picture Vettiyanakan · Mar 23, 2015 · Viewed 213.8k times · Source

How can I hide a navigation bar from first ViewController or a particular ViewController in swift?

I used the following code in viewDidLoad():

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.isNavigationBarHidden = true
}

and also on viewWillAppear:

override func viewWillAppear(animated: Bool) {
    self.navigationController?.isNavigationBarHidden = true
}

Both methods hide the navigation controller from all ViewControllers.

Answer

Rengers picture Rengers · Mar 23, 2015

If you know that all other views should have the bar visible, you could use viewWillDisappear to set it to visible again.

In Swift:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    navigationController?.setNavigationBarHidden(true, animated: animated)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    navigationController?.setNavigationBarHidden(false, animated: animated)
}