Search code examples
iosxcodeuiviewcontrolleruinavigationcontrolleruikit

iOS top left back button without visible navigation bar


I am developping an iOS application with UIKit and a Storyboard in Xcode 11.1.

I have a ViewController (embedded in a Navigation Controller) that contains a fullscreen MKMapView. I would really like this MKMapView to take as much space as possible, so I choose to hide the navigation bar of the Navigation Controller. However from the View Controller I push another View Controller, but as the navigation bar is hidden, this pushed View Controller has no back button in the top left corner because the previous View Controller has no / a hidden navigation bar.

How can I make the back button on the pushed View Controller visible without having to make the navigation bar on the View Controller it is being pushed from visible?

I would not like to hardcode some button on the second, pushed View Controller, as it is also accessed by other View Controllers, which have a visible navigation bar and thus the back button is shown for them

Here is a small diagram that shows my setup. enter image description here


Solution

  • add this to your view controller that contains MKMapView & has no navigationBar:

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