How To Set Back Button Text In Swift

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 and want to learn more, check out The Complete iOS Developer Bootcamp. Speed up your learning curve - hundreds of students have already joined. Thanks for reading!

Eddy Chung

I teach iOS development on ZeroToAppStore.com.

Similar Posts