0

I would like to change the BackBarButtonItem on all the views in my iOS app so that they show just the back arrow with no text.

Is the only way to accomplish this is to go through every UIViewController and set the self.navigationItem.backBarButtonItem.title ? Seems I should be able to create a superclass to define the default value of backBarButtonItem.title, but I am not sure where to start.

Thank you for your help.

Chris
  • 5,485
  • 15
  • 68
  • 130
  • Have you tried a category on UIViewController? – Simon Jan 13 '16 at 18:11
  • I think if I do a category, I would have the #import that category into every Navigation Controller, correct? I am currently looking into Swizzling. – Chris Jan 13 '16 at 18:26
  • No, you wouldn't need to #import it. – Simon Jan 13 '16 at 19:37
  • Can you point me to some more information about this? How would I implement this so it effects all `BackBarButtonItems` without having to call my category method every time the app creates a new back button? – Chris Jan 13 '16 at 21:48
  • If you create a category on UIViewController and override `backBarButtonItem` in it, then your method will be called whenever their back bar button item is called for. You only need to swizzle if you want to call the superclass method. – Simon Jan 13 '16 at 22:04
  • Are you sure the category should be on `UIViewController` ? `backBarButtonitem` is a property of `UINavigationItem` ... – Chris Jan 13 '16 at 23:44
  • Also, are you sure that a `category` is what I should be implementing? Everything I am reading about `categories` states that they are used to add methods to a existing class - not finding anything about overriding properties in the existing class. – Chris Jan 14 '16 at 00:44
  • you're right, it should be on UINavigationItem. Categories are usually used to add methods but can be used to replace them. – Simon Jan 14 '16 at 07:37
  • Thank You - How do I determine which method to override? And do I just ignore the warning that appears, which states that the method already exists / could causes clashes? – Chris Jan 14 '16 at 14:41
  • You override `backBarButtonItem`. I don't see why you'd get such a warning. – Simon Jan 14 '16 at 17:25
  • @Simon Please see this post http://stackoverflow.com/q/34793366/300129 – Chris Jan 15 '16 at 16:46

2 Answers2

1

You don't need to change it on every view controller class. You can either sublcass it and change once, or you can do it in your app delegate.

If you want to subclass the Navigation Bar, you can simple set the back button with text @"":

  UIBarButtonItem *newBackButton = 
  [[UIBarButtonItem alloc] initWithTitle:@"" 
                                     style:UIBarButtonItemStylePlain 
                                    target:nil 
                                    action:nil];
  [[self navigationItem] setBackBarButtonItem:newBackButton];

If you want to do it in the app delegate you can do it in two ways:

One way is to set the button text color as clear color:

[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor clearColor]} forState:UIControlStateNormal];

Another way is to position the back button away from the screen, so user can't see it

    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -200) forBarMetrics:UIBarMetricsDefault];
Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
  • Won't the `[UIBarButtonItem appearance]` attributes cause all Navigation Bar Buttons, not just `back` buttons, to hide their titles? – Chris Jan 13 '16 at 21:48
  • Yes, You are right, that is because I didnt mention the back button for that line of code. If you are using appearence, that is the two ways available at the moment. Apple needs to update the API to set the back button title by using appearence. – Teja Nandamuri Jan 13 '16 at 21:50
  • Do you have a Swift version? I'm trying to translate, but can't achieve it. – jo3birdtalk Mar 01 '16 at 09:13
-1

we can implement delegate function of navigation controller and set delegate for view controller implementing this function:

1) navigationController.delegate = yourViewController

2) Delegate function :

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}

This will remove backbutton title text of all following view controllers

CodeMatrix
  • 2,124
  • 1
  • 18
  • 30