Search code examples
iosswiftuinavigationcontrolleruinavigationbar

move backButton from default UINavigationbar to customNavigationbar in swift


In viewWillAppear i hide my navigationController and navigationBar like this:

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: true)
        self.navigationController?.interactivePopGestureRecognizer?.delegate = nil
}

Then i added a navigationBar in my Storyboard and connected it to an IBOutlet in my UIViewController to customize it from IB.

Everything is working fine except i can not move default backButton from the original hidden navigatioBar to my new custom navigationBar.

Question:

Is there a way to move backButton from default navigationBar to this new custom navigationBar?**

Note: I don't want to add a customized back button.


Solution

  • You can't. You'd need to create your own UIBarButtonItem

    let backItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(yourSelector))
    self.navigationItem.leftBarButtonItem = backItem