swiftxcodeuinavigationbaruibarbuttonitembackbarbuttonitem

Left Bar Button Item - show name of previous View Controller


If you have a Navigation Controller with no Bar Button Items, a navigation back button will be shown with the name of the last View Controller.

I want to keep that name, as in I don't want to have to hardcode it. I do know how to add it in but I don't want to have to do that because that leaves more room for bugs.

Is there a way that I can have a left Bar Button Item and for the default one to not go away?


Solution

  • Add this in viewController where you want to have default back button and custom bar button item. You can customise the bar button item.

    override func viewDidLoad() {
        super.viewDidLoad()
        let newBtn = UIBarButtonItem(title: "new", style: .plain, target: self, action: #selector(anotherMethod))
        self.navigationItem.leftItemsSupplementBackButton = true
        self.navigationItem.leftBarButtonItem = newBtn//self.navigationItem.leftBarButtonItems = [newBtn,anotherBtn]
    }