1

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.

Community
  • 1
  • 1
sonxurxo
  • 5,648
  • 2
  • 23
  • 33

4 Answers4

4

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.

Ibrahim Yildirim
  • 2,731
  • 2
  • 19
  • 31
  • +1 for giving a valid solution, though having to extend all view controllers from a custom one is quite annoying. More, I'll have to implement `UITableViewController` custom class too. But nice approach, thank you – sonxurxo Oct 22 '15 at 16:10
  • Well you can make a ```UIViewController Extension``` (category in obj-c) with a method ```removeTitleFromBackButton()``` and then call ```self.removeTitleFromBackButton()``` in the viewControllers. – Ibrahim Yildirim Oct 22 '15 at 17:29
  • Finally accepted this answer as the only feasible. Thank you @IbrahimYildirim – sonxurxo Nov 17 '15 at 16:57
3

try with this

self.navigationItem.hidesBackButton = YES;

to hide explicit(that you set yourself) back button

self.navigationItem.leftBarButtonItem = nil;
Jamil
  • 2,977
  • 1
  • 13
  • 23
0

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 " "

Moriya
  • 7,750
  • 3
  • 35
  • 53
  • I can't see how that affects the back button – sonxurxo Oct 22 '15 at 16:03
  • Back buttons title is set from previous view controllers title property. If the title is " " the buttons text is " ". I did forget to mention the title property needs to be set to " " though – Moriya Oct 23 '15 at 00:00
0

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.

Juan Ramirez
  • 51
  • 1
  • 4