Reputation: 143
I want to get rid of the title on a navigation bar's back button. It keeps crowding the title of my view controller.
How do I hide it using Swift 3?
This doesn't work:
self.navigationController?.navigationItem.backBarButtonItem?.title = nil
Upvotes: 0
Views: 864
Reputation: 143
THIS WORKS:
I had to put:
self.navigationItem.title = ""
That's it. If anybody can explain why this works, and the other posted answers don't, that'd be great!
Upvotes: 1
Reputation: 19156
Lets say you have A
ViewController pushes B
ViewController. And you wan't to remove the back button title in ViewController B
. Then You need to set the backBarButtonItem
of the A
ViewController. Something like this.
class AViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
}
}
Upvotes: 0
Reputation: 7419
Check this out:
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
}
More detail: https://stackoverflow.com/a/32284525/4935811
Upvotes: 0
Reputation: 3499
Add either
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
or
self.navigationController?.navigationBar.backItem?.title = ""
to your prepare:forsegue:
if you use storyboard or before navigating.
Upvotes: 1
Reputation: 6795
It works for me . . replace nil
with ""
(Swift 3)
self.navigationController?.navigationItem.backBarButtonItem?.title = ""
Upvotes: 0