How to set back button text in Swift
You can do it from interface builder as follows:
click on the navigation item of previous view controller
from the attributes inspector set the back button text to whatever you want. Thats it!!
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)
You can put this 3 line of code in the ViewController
you want to change the back button title.
In your override func viewDidLoad() {}
.
let backButton = UIBarButtonItem()
backButton.title = "My Back Button Title"
self.navigationController?.navigationBar.topItem?.backBarButtonItem = backButton