Search code examples
iosswiftuiviewcontrolleruinavigationcontrollermodalviewcontroller

How to modally present a navigation controller in Swift?


I am trying to present a navigation controller modally,the thing is my navigation bar is not showing any baritems... any ideas? This is my code. If I add a backgroud color to the navigation bar it actually shows, but not the buttons... Please help!

if let navigationController = baseController as? UINavigationController {
    if UIScreen.main.traitCollection.userInterfaceIdiom == .phone {
        navigationController.present(webViewController, animated: true, completion: nil)
    } else {
        let webNavigationController = UINavigationController(rootViewController: webViewController)
        webNavigationController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(cancelTapped))
        webNavigationController.isNavigationBarHidden = false
        webNavigationController.modalPresentationStyle = .overFullScreen
        navigationController.present(webNavigationController, animated: true, completion: nil)
    }
}

Solution

  • This line is wrong:

    webNavigationController.navigationItem.leftBarButtonItem = UIBarButtonItem(...
    

    It is not webNavigationController whose navigationItem dictates the presence of bar button items, but rather webViewController.