sonxurxo
sonxurxo

Reputation: 5718

Remove UINavigationBar back button title

Is there a way to remove the title of back button items in all view controllers of an app?


Note: The important thing is the all word. The closest I've been is with this solution:

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                     forBarMetrics:UIBarMetricsDefault];

but as I mention in the comment there's an issue with large titles being pushed to the right so that this solution is not valid.

Upvotes: 1

Views: 2118

Answers (4)

Juan Ramirez
Juan Ramirez

Reputation: 51

All you need to change the title of the back button of a UINavigationController is:

Swift:

navigationController?.navigationBar.topItem?.title = ""

You can add this to viewWillAppear of the new viewController being pushed or in a custom base class of UIViewController.

Upvotes: 0

Moriya
Moriya

Reputation: 7906

One way of doing this that hasn't been mentioned is to use a custom tile view that displays the title in a label and then never actually setting the title property of the ViewControllers.

This should be set up in either an extension of the UIViewController or a base class.

Then you don't have to change the barButtonItem at all and you have options to do more changes to the title (with subtitles etc.)

Something like this:

extension UIViewController {

    func setTitleView(title:String) {
        let titleView = CustomTitleView()
        titleView.titleLabel.text = title
        self.navigationItem.titleView = titleView
    }
}

If you set up a simple CustomTitleView nib with one label outlet and some constraints.

Edit:

Title property needs to be set to " " on all the VC. That way the back buttons text in the next pushed VC will be titled " "

Upvotes: 0

Jamil
Jamil

Reputation: 2999

try with this

self.navigationItem.hidesBackButton = YES;

to hide explicit(that you set yourself) back button

self.navigationItem.leftBarButtonItem = nil;

Upvotes: 3

Ibrahim Yildirim
Ibrahim Yildirim

Reputation: 2771

Just set the title of the back button to ""

Here's a example in swift

self.navigationItem.backBarButtonItem = UIBarButtonItem(
        title: "",
        style: UIBarButtonItemStyle.Plain,
        target: nil,
        action: nil)

To do it in all the view controllers, i usually make a BaseViewController where i include that line in the viewDidLoad method. Then when i have a viewcontroller i subclass it to the BaseViewController.

Your solution could work if you also move the x value -150, but for me it sounds like hack because you are still showing it all the time, but off the view bounds.

Upvotes: 4

Related Questions