Search code examples
swiftuinavigationcontrolleruibarbuttonitemnavigationbarpushviewcontroller

Push Navigation Controller Without Title On Back Button


I currently have view controller A and view controller B.

View Controller A pushes vc B onto it's navigationController on a button press:

navigationController?.pushViewController(viewControllerB, animated: true)

VC A has a navigation bar that has a title on it "View Controller A Title"

I want VC B to have a left button, which is a back button, and a title, "View Controller B Title"

Currently, when I push VC B onto the nav controller, the title of VC A comes along with it onto the left back arrow, so it looks like

< View Controller A Title View Controller B Title

How do I get rid of the VC A Title?

I've tried setting left bar button item, back bar button item, both with no success.

The only way I've found to remove that title is to do

navigationController?.navigationBar.items?.forEach({ $0.title = "" }) inside of the viewDidLoad of VC B, but then when I click the back arrow, the title is of course missing from VC A

The only solution I've found for this solution listed above here, is to set the navigationItem.title in the viewDidAppear of VC A, but there's about a 1/2 second delay from the title actually showing up which is not ideal.

Is there a better way to not have this title come along with the navigation left bar item?

Thank you in advance!


Solution

  • In View Controller A, just set the backBarButtonItem.

    navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)