I have an app with the following structure:
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.
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.