Search code examples
iosswiftuinavigationcontrolleruinavigationbar

How do I instantly hide the navigation bar on a single view controller?


I have an app with two view controllers and an image at the top of the screen. I've hidden the navigation bar on the first (main) view controller only with no problem but using the "Back" button from the second view controller causes my image to briefly drop down as the navigation bar is hidden. I'd like to return to the first screen without the image moving at all if possible. The code I'm using to hide the navigation bar is below:

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

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

I'm using a single storyboard. Any suggestions?


Solution

  • In view will disappear try this

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

    remove animation while hiding unhiding navigation bar.