iOS 7 Tint Color - UINavigationItem's backBarButtonItem doesn't tint when initWithImage:

edelaney05 picture edelaney05 · Sep 26, 2013 · Viewed 9.7k times · Source

I'm having trouble applying a tint color the navigation item's back bar button item when I create the bar button item with -[UIBarButtonItem initWithImage:style:target:selector:].

Is using an image as a view controller back context no longer okay? I can't seem to find any indication in the HIG or else where this has been deprecated or discouraged.

Here's my code:

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"camera-navigation_item_back"]
                                                                  style:UIBarButtonItemStyleBordered
                                                                 target:nil
                                                                 action:nil];
[navItem setBackBarButtonItem:barButtonItem];

iOS 7 Result:

iOS 7

iOS 6 Result:

iOS 6


EDIT: If I try to use one of the system items (plus sign, trash can, etc) as my back button, Apple substitutes the image for the title "Back." This is actually the same behavior in iOS 6 and 7.

Answer

carton picture carton · Sep 29, 2013

Set the image's rendering mode to UIImageReneringModeAlwaysTemplate (this topic is covered at around 33:00 in the WWDC video mentioned in the previous answer):

UIImage *backButtonImage = [UIImage imageNamed:@"imageName.png"];
backButtonImage = [backButtonImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIBarButtonItem * backButton = [[UIBarButtonItem alloc]
                               initWithImage:backButtonImage
                               style:UIBarButtonItemStylePlain
                               target:nil
                               action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];
[[[self navigationItem] backBarButtonItem] setTintColor:[UIColor redColor]];

The last line is not necessary if you have set the tintColor globally in AppDelegate.h:

[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];