Charles Black
Charles Black

Reputation: 143

How do I delete a UINavigationController's back button title?

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

Answers (5)

Charles Black
Charles Black

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

Bilal
Bilal

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

Sour LeangChhean
Sour LeangChhean

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

Lawliet
Lawliet

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

Sudipto Roy
Sudipto Roy

Reputation: 6795

It works for me . . replace nil with "" (Swift 3)

 self.navigationController?.navigationItem.backBarButtonItem?.title = "" 

Upvotes: 0

Related Questions