1

I am not able to show a back bar button item in my navigation item in my root view controller of my navigation controller.

I have tried setting different properties.

Other questions like this do not give me an answer that works.

Her is my code in my root view controller:

override func viewDidLoad() {
    super.viewDidLoad()

    print("viewDidLoad")

    navigationItem.leftItemsSupplementBackButton = true
    navigationItem.backBarButtonItem = UIBarButtonItem(title: "Button", style: .plain, target: nil, action: nil)
    navigationItem.setHidesBackButton(false, animated: true)
    navigationItem.leftItemsSupplementBackButton = true

}
daniel
  • 1,446
  • 3
  • 29
  • 65

1 Answers1

0

Assigning to leftBarButtonItem instead of backBarButtonItem works for me:

override func viewDidLoad() {
    super.viewDidLoad()

    print("viewDidLoad")

    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Button", style: .plain, target: nil, action: nil)
}
dlggr
  • 741
  • 7
  • 15
  • I don’t think that is the way Apple intended. Done the correct way, there would be added functionality to the back button, like a left-arrow image next to the button label, the button label would be automatically set, and the user would be taken back to the previous scene when the button is tapped. I can get it to work the way Apple intended when using a split view controller. – daniel Oct 20 '18 at 18:55
  • Where should the user be taken though, if, as stated in the question, the view controller is the root view controller in the navigation hierarchy? – dlggr Oct 20 '18 at 19:01
  • The root view controller is the view controller attached to the navigation controller. It is not necessarily the initial view controller that the user is taken to at app launch. The user would be taken back to the view controller that presented the current view controller. – daniel Oct 20 '18 at 19:07
  • 1
    Yes, that's what I meant. The `rootViewController` of the navigation controller. As stated in the [docs](https://developer.apple.com/documentation/uikit/uinavigationcontroller): "_For all but the root view, the navigation controller provides a back button to allow the user to move back up the hierarchy._" If you want to dismiss the whole navigation stack that the navigation controller handles, you can just dismiss the navigation controller from the view controller that initially presented it. – dlggr Oct 20 '18 at 19:14
  • I think I’ve been using the navigation controller wrong. Thanks for showing that to me. I appreciate it. – daniel Oct 20 '18 at 21:32