0

I've ran into an interesting and strange question while messing around with a project.

After spending like 3 hours doing it, I found out you can't change the view of the UIBackBarButtonItem, only the UILeftBarButtonItem, so if I want to implement a custom back button, I hide the UIBackButtonItem and display a UILeftBarButtonItem which does the popping.

Now I find it odd, that you can't change the UIBackBarButtonItem's view, but you can change the UILeftBarButtonItem and the UIRightBarButtonItem's views.

Can someone explain me why would Apple do this?

Lord Zsolt
  • 6,492
  • 9
  • 46
  • 76

3 Answers3

0

Actually, you can. Use UIBarButtonItem's instance method setBackButtonBackgroundImage:forState:barMetrics:.

So if you want to change the background image for all your back buttons, your code should like something like:

UIImage *backButtonBgImageNormal = [[UIImage imageNamed:@"back_button_bg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 15, 5, 5);];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonBgImageNormal forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Guillaume Algis
  • 10,705
  • 6
  • 44
  • 72
  • Yes you can do that, but if you do so, it puts an image and puts the title text above it (Annoying when you have image with text on it (I know you shouldn't for localization, but yeah)), thus looking REALLY stupid. And if you set the title to "" or nil, the button doesn't show. – Lord Zsolt Jul 26 '13 at 09:14
  • Couldn't you set the back button title to an empty string ? http://stackoverflow.com/questions/2197698/how-to-set-the-text-of-a-back-button-on-a-uinavigationbar – Guillaume Algis Jul 26 '13 at 09:18
  • Read my comment again – Lord Zsolt Jul 26 '13 at 09:19
0

Use delegate method of UINavigationController, such like

#pragma mark -
#pragma mark - UINavigationController Delegate Methods

- (void)navigationController:(UINavigationController *)navigationController  willShowViewController:(UIViewController *)viewController  animated:(BOOL)animated
{
   //// customize you own stuff here;

    UINavigationBar *morenavbar = navigationController.navigationBar;
    UINavigationItem *morenavitem = morenavbar.topItem;
    morenavitem.rightBarButtonItem.tintColor = [UIColor blackColor];   
}
0

I think I have a comprehensive solution for you.

1) From experience, it's just best to not bow to limitations of BarButtonItems. I suggest you create a simple UIButton, customise it to your liking. maybe add an action to its touch events..brand it with custom background and title colors...etc.. keep a reference to this button, maybe as a property.

2) Then, you create an instance of UIBarButtonItem using the UIBarButtonItem initializer -initWithCustomView, you sneak in the UIButton instance as this custom view in the init and have complete control over it.

3) Finally, you just do this. self.navigationItem.LeftBarButtonItems = @[ourUIBarButtonItem].

The navigation bar has an Array property "leftBarButtonItems" for series of left buttons, and rightBarbuttonItems for the right side. Just replace this with your own array, containing the UIbarButtonItem, that containing your button, and you having a reference to your button.

And now you can completely control you button that is properly in the navigation bar.

NOTE! - Once you provide such a leftBarButtonItem, the stock Apple BackButton is gone.

Earl Grey
  • 7,426
  • 6
  • 39
  • 59