3

I've set an image to be the background image for all nav bar back buttons in my app using the appearance property. I do it with the following code in the applicationDidLoad method.

UIImage *backButton = [UIImage imageNamed:@"btn-nav-back"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButton forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

My problem is that whenever the back button appears the image is stretched due to the name of the previous view being set as the back button's title. Is there a system wide way I can stop the title being set? Also attempting to set the back button's title to blank in the actual view controllers has no effect :(

[self.navigationItem.backBarButtonItem setTitle:@""];
bennythemink
  • 5,096
  • 4
  • 36
  • 54

7 Answers7

9

I found a way to achieve what you want.

First make your back image resizable, so that it wont be stretched to the length of the back button:

UIImage* backImage = [[UIImage imageNamed:@"back"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 0)];

Set this image as backButtonBackgroundImage via UIAppearance:

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

Last, move the title out of the visible area:

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

Only downside is, that the width of the backButton isn't changed, so that your UINavigationBar title can be off center.

pre
  • 3,475
  • 2
  • 28
  • 43
2
UIBarButtonItem *customItem = [[[UIBarButtonItem alloc] initWithCustomView:plainButton] autorelease];
self.navigationItem.leftBarButtonItem = customItem; 
[(UIButton*)self.navigationItem.leftBarButtonItem.customView addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];

Make a custom barButtonItem and exchange it for the default back button.

BabyPanda
  • 1,562
  • 12
  • 18
  • Thanks BabyPanda but I wished to be able to set it system wide and not just per view. – bennythemink Aug 16 '12 at 07:31
  • by "system wide", if you mean set it for all the viewControllers, then you could make a category like `UIViewController+CustomNavigation` and implement the category method `- (void)setLeftBarButtonItem:(UIButton*)plainButton;` in the viewControllers – BabyPanda Aug 16 '12 at 08:01
  • Hey BabyPanda, thats indeed what I was doing but I just wondered if there was a way to do it using the appearance property for the nav bar. I don't really like having to import and call the category for each class, was hoping there might be a "cleaner" way to do it – bennythemink Aug 20 '12 at 04:00
  • well, shame that i haven't found a way to barely implement navigationbar appearance to do the work yet... – BabyPanda Aug 20 '12 at 06:29
1

Found this question that tells me unfortunately I cannot do what I wish :(

Can you use UIAppearance to set the titleview of UINavigationItem?

Community
  • 1
  • 1
bennythemink
  • 5,096
  • 4
  • 36
  • 54
1

You simply can do this without using a back bar button image as well if you need the same appearance as default BackBarButton image. Just write the following lines before you navigate to other controller:

  UIBarButtonItem *backButton = [[[UIBarButtonItem alloc]
      initWithTitle:@"Back"
              style:UIBarButtonItemStyleBordered
             target:nil
             action:nil] autorelease];
  [[viewcontroller navigationItem] setBackBarButtonItem:backButton];  

This will solve your problem. Also, you will be able to change the image as per your need using another method of barButton. Please let me know if you find any problem or you were looking for another problem.

Mathew Varghese
  • 4,527
  • 2
  • 17
  • 26
Tarun
  • 766
  • 1
  • 5
  • 12
1

The way i got around it was:

    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.clearColor()], forState: UIControlState.Normal)

This works on iOS 8.

stephen
  • 1,617
  • 3
  • 20
  • 27
1

I know it's an old post, but if I had come across the post, there must be others too looking for it.

Just adding to Stephen's answer: This makes text white for Normal and Selected state. This makes it system wide with just 2 lines. However, the image takes up the space for white text and looks quite bad.

For system wide back button, you use the code from above

UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.clearColor()], forState: UIControlState.Normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.clearColor()], forState: UIControlState.Highlighted)
Sategroup
  • 955
  • 13
  • 29
  • This works but the only problem is that it impacts **all** `UIBarButtonItems`, not just the back button. – Sid Aug 14 '15 at 02:08
0

Swift 3.x

Add this line in AppDelegate file
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)