Reputation: 2597
I'd like to set the back button title to just "Back". I've set the title "Back" in the storyboard (Navigation Item -> Back Button) and it appears to be working on iPhone X.
While testing on iPhone SE, I see the previous VC title as the back button title. So now it displays like "< APP NAME.......APP NAME......." which is a bit odd.
It'd have made sense if the iPhone X displayed it like above, because the screen width is longer, therefor it has more space for a longer title.
What could I do to make this work? I've already tried some things like: Subclass UINavigation controller and add:
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)
Do the above, but directly in the View Controllers.
Add spaces around the View Controller title to make it longer (doesn't work, and if it did than it's a bit hacky).
Any help would be appreciated.
Upvotes: 1
Views: 304
Reputation: 53183
Your code to set the backBarButtonItem
is fine, but you're setting it on the wrong view controller.
If you have 2 view controllers VCA and VCB, and VCA pushes VCB onto it's navigation controller's nav stack, you need to set the backBarButtonItem
in VCA, before pushing on VCB. You can do this in your viewDidLoad
method
class VCA: UIViewController {
func viewDidLoad() {
super.viewDidLoad()
navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)
}
}
Upvotes: 2
Reputation: 492
To modify the back button you should update it before pushing, on the view controller that initiated the segue:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let backItem = UIBarButtonItem()
backItem.title = "Back"
navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed
}
Upvotes: 2