Search code examples
iosswiftuinavigationcontrolleruinavigationbaruicontainerview

Main Navigation Bar Disappears When the App Is Suspended


I have an app with the following structure:

  1. At the root of my application is a main scene with a basic view controller managed by my top-level navigation controller and displaying a navigation bar.
  2. I have a second scene with a container view occupying the entire screen.
  3. Embedded in the container view is another navigation controller with additional view controllers and its own navigation bar.
  4. To avoid both navigation bars being shown at the same time I hide and unhide the main navigation bar in the container view controller as shown in the code below.
  5. I navigate from the main scene to the embedded scene and back.
override func viewDidLoad() {
    super.viewDidLoad()
    // Get notified when app is resumed.
    NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
    // Hide main navigation controller's top bar.
    navigationController?.setNavigationBarHidden(true, animated: true)
}

override func viewWillDisappear(_ animated: Bool) {
    // Show main navigation controller's top bar when leaving split view.
    self.navigationController?.setNavigationBarHidden(false, animated: true)
    super.viewWillDisappear(animated)
}

func applicationDidBecomeActive() {
    // Hide main navigation controller's top bar after returning to app.
    navigationController?.navigationBar.isHidden = true
}

This works well as long as I do not leave the app. If the app gets dismissed while showing the embedded view the following happens: The embedded navigation bar is still visible but when I return to the main view its navigation bar is gone.

Additional attempts to unhide it in the main scene do not bring it back. How can I get the navigation bar to show?

On a side note, in case you want to see the more detailed setup it is described in a previous question.


Update: This is odd. I have found three ways to hide a navigation bar, and they all seem to do the job under most circumstances:

navigationController?.isNavigationBarHidden = true navigationController?.setNavigationBarHidden(true, animated: false) navigationController?.navigationBar.isHidden = true

The last one is what I had used to ensure the bar stays hidden after the app resumes (see applicationDidBecomeActive() above). Replacing it with either of the other two options eliminates the problem. It had nothing to do with the embedded navigation controller.

I don't dare to post this as an answer yet because I can't really explain it. If anyone can I'll be happy to accept the answer.


Solution

  • The problem seems to be that all three mentioned ways are capable of hiding the navigation bar from the view but the third option is setting a different property than the other two.

    Therefore, hiding the bar with navigationController?.navigationBar.isHidden = true and then trying to unhide it with self.navigationController?.setNavigationBarHidden(false, animated: true) does not work. Being consistent either way solves the problem.