Search code examples
iosswiftuinavigationbaruiviewanimationtransition

How to have a better transition between controllers for the navigation bar?


When I'm in the 2nd view controller and I press the back button, the navigation bar disappears brutally. I would like it to slide away from the screen with the view controller. And when I press a button to go to the 2nd view controller, the navigation bar appears before the 2nd view controller is on screen.

  • Is there a way to have a smoother transition of the navigation bar ?
  • Can you have a custom transition for the navigation bar ?
var previousStatusBarHiddenState: Bool = false

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

      // store navigationBar hidden state
      if let navigationBar = navigationController?.navigationBar {
      previousStatusBarHiddenState = navigationBar.hidden
      navigationBar.hidden = true
     }

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

     // restore navigationBar hidden state
     if let navigationBar = navigationController?.navigationBar {
     navigationBar.hidden = previousStatusBarHiddenState
    }

Solution

  • I found the solution :

    I used navigationController.setNavigationBarHidden(true, animated: animated)

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated) 
    
           if let navigationController = navigationController {
           previousStatusBarHiddenState = navigationController.navigationBar.hidden
           navigationController.setNavigationBarHidden(true, animated: animated)
      }
    }
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
    
           // restore status bar
           navigationController?.setNavigationBarHidden(previousStatusBarHiddenState, animated: animated)
    }