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

267,607

Solution 1

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)
}

Solution 2

Swift 3

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Hide the navigation bar on the this view controller
    self.navigationController?.setNavigationBarHidden(true, animated: animated)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    // Show the navigation bar on other view controllers
    self.navigationController?.setNavigationBarHidden(false, animated: animated)
}

Solution 3

You can unhide navigationController in viewWillDisappear

override func viewWillDisappear(animated: Bool)
{
    super.viewWillDisappear(animated)
    self.navigationController?.isNavigationBarHidden = false
}

Swift 3

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    self.navigationController?.setNavigationBarHidden(false, animated: animated)
}

Solution 4

You could also create an extension for this so you will be able to reuse the extension without implementing this again and again in every view controller.

import UIKit

extension UIViewController {
    func hideNavigationBar(animated: Bool){
        // Hide the navigation bar on the this view controller
        self.navigationController?.setNavigationBarHidden(true, animated: animated)

    }

    func showNavigationBar(animated: Bool) {
        // Show the navigation bar on other view controllers
        self.navigationController?.setNavigationBarHidden(false, animated: animated)
    }

}

So you can use the extension methods as below

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        hideNavigationBar(animated: animated)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        showNavigationBar(animated: animated)
    }

Solution 5

In Swift 3, you can use isNavigationBarHidden Property also to show or hide navigation bar

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // Hide the navigation bar for current view controller
    self.navigationController?.isNavigationBarHidden = true;
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    // Show the navigation bar on other view controllers
   self.navigationController?.isNavigationBarHidden = false;
}
Share:
267,607

Related videos on Youtube

Vettiyanakan
Author by

Vettiyanakan

Previously Web, IOS and Android developer, Now Fluttering with Dart.

Updated on July 08, 2022

Comments

  • Vettiyanakan
    Vettiyanakan almost 2 years

    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.

    • itsji10dra
      itsji10dra about 9 years
      you need to manage it manually for all viewcontrollers.. you can't do it for any one..
  • NielsKoole
    NielsKoole over 7 years
    With overriding don't forget to call the super methods: super.viewWillAppear(animated) and super.viewWillDisappear(animated)
  • Amr Eladawy
    Amr Eladawy over 7 years
    This answer is more efficient . Think of the repetitive code with each new ViewController you add. stackoverflow.com/a/39679506/5079380
  • MBH
    MBH almost 7 years
    what is the Window Controller ?
  • Joris Weimar
    Joris Weimar over 6 years
    Not really worth the extension, is it? :)
  • jnwagstaff
    jnwagstaff over 6 years
    Depends on how many views you are hiding/showing the nav bars. I feel like most cases you only hide the first one but if you do it a lot the extension is nice.
  • Cons Bulaquena
    Cons Bulaquena over 6 years
    Does it remove the link that says back?
  • zalogatomek
    zalogatomek about 6 years
    I was convinced that it would not work well with the "swipe back" on the visual level, but everything is fine. Thanks!
  • Tomasz Pe
    Tomasz Pe about 6 years
    Side note: self. not needed.
  • T.Okahara
    T.Okahara about 6 years
    On the swipe back, from the view with the navigation bar to the view with the hidden navigation bar, how do we reimplement the navigation bar fading?
  • Thafer Shahin
    Thafer Shahin almost 6 years
    Definitely, it doesn't worth it. Don't invent something already exists.
  • Nate Uni
    Nate Uni over 5 years
    self.navigationController?.setNavigationBarHidden(true, animated: true) worked for me
  • Koen.
    Koen. over 4 years
    This is for macOS, not iOS
  • Radek Wilczak
    Radek Wilczak over 4 years
    When I present modal on it from the bottom, the animation is showing the navigation bar with animation, and it's looks bad...
  • Starsky
    Starsky about 4 years
    This is a copy/paste response. What is the difference between your response and the other 2 or 3 equal responses here??