Search code examples
iosuikituinavigationcontrolleruinavigationitem

How to set back button text in iOS navigation controller?


How do you remove the back button text.

Current back button:

< Back

Desired back button:

< AnythingElse

None of these have worked:

self.navigationItem.backBarButtonItem?.title = "Back"
self.backItem?.title = ""
self.navigationController?.navigationBar.backItem?.title = ""
self.navigationItem.backBarButtonItem?.title = ""
self.navigationController?.navigationItem.backBarButtonItem?.title="Back"
self.navigationController?.navigationBar.backItem?.title = ""
self.navigationController?.navigationItem.backBarButtonItem?.title

Solution

  • The back button belongs to the previous view controller, not the one currently presented on screen.
    To modify the back button you should update it before pushing, on the view controller that initiated the segue:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let backItem = UIBarButtonItem()
        backItem.title = "Something Else"
        navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed
    }
    

    Swift 3, 4 & 5:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let backItem = UIBarButtonItem()
        backItem.title = "Something Else"
        navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed
    }
    

    OR

    // in your viewDidLoad or viewWillAppear
    navigationItem.backBarButtonItem = UIBarButtonItem(
        title: "Something Else", style: .plain, target: nil, action: nil)