How To Set Back Button Text In Swift

By Eddy Chung

To change the back button text of the current view, add the following code to your viewDidAppear:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let backBarBtnItem = UIBarButtonItem()
    backBarBtnItem.title = "Different Text!"
    navigationController?.navigationBar.backItem?.backBarButtonItem = backBarBtnItem
}

This is what it will look like:

how to set back button text swift 2020 03 08 19 27 33

Change The Back Button Title In The Previous View Controller

If you would like to change the navigation back button title in the previous view controller, use this code in your viewDidLoad():

override func viewDidLoad() {
    super.viewDidLoad()
    let backBarBtnItem = UIBarButtonItem()
    backBarBtnItem.title = "something different"
    navigationItem.backBarButtonItem = backBarBtnItem
}

how to set back button text swift 2020 03 08 19 38 10

You can also just set the title of the previous view controller and iOS will automatically set that to the back button of the view controller.

For example, we can set our title to be “awesome view controller”:

override func viewDidLoad() {
    super.viewDidLoad()
    title = "nice!"
    navigationController?.pushViewController(AnotherViewController(), animated: true)
}

how to set back button text swift 2020 03 08 19 40 26

Now on the next view controller that push on the navigation stack, it will have this title as its back button.

how to set back button text swift 2020 03 08 19 46 19

If you liked this post, you'll love my free guide: Secrets To iOS Development. Speed up your learning curve - hundreds of students have already downloaded. Thanks for reading!
Profile Picture of Eddy Chung

Eddy Chung

I am a professional iOS developer in Silicon Valley. I teach iOS development on ZeroToAppStore.com. If you'd like to learn more about me click here or you can contact me at: eddy@zerotoappstore.com

Similar Posts