13

I want to change the color of back button of a navigation bar to make it look like thisenter image description here

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
Nilesh Tupe
  • 7,655
  • 5
  • 25
  • 30

5 Answers5

63

Set the backBarButtonItem's tintColor:

self.navigationItem.backBarButtonItem.tintColor = [UIColor redColor];

TIP: If you want this to be applied to all UIBarButtonItem instances in your application by default, then you can use the new UIAppearance API to do just that:

[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • I tried this- (void)viewDidLoad { [super viewDidLoad]; UIImageView *image=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"logobar.png"]]; NSLog(@"Frame==%f",image.frame.size.width); self.navigationItem.backBarButtonItem.tintColor = [UIColor redColor]; self.navigationItem.titleView= image; NSLog(@"%f",self.navigationItem.titleView.frame.size.width); } but it is not working – Nilesh Tupe Oct 28 '11 at 12:48
  • [[UIBarButtonItem appearance] setTintColor:[UIColor redColor]]; This method is present for IOS 5.0 or above how to make it work for lower versions? – Nilesh Tupe Oct 31 '11 at 09:18
  • @NileshTupe You will need to use the other line of code that I've provided, which should work in the context of the pushed view controller. – Jacob Relkin Oct 31 '11 at 09:24
  • I have put that code in viewdidload method of pushed view controller.But it has no effect.Where should I put that line ? – Nilesh Tupe Oct 31 '11 at 09:49
  • You shouldn't put the first line in viewDidLoad of the pushed view controller. You should put it in viewDidLoad of the PUSHING view controller, and all controllers pushed from it will have the correct back button. – Accatyyc Dec 04 '12 at 09:49
8

The first line of jacob's answer didn't work for me because the backBarButtonItem was NULL. It is NULL because it's been created later automatically when switching to an other ViewController. At that time you can set the title of the button with

self.title = @"nice title"; // self is here the view(controller) within the popoverController

but you can't set the tintColor.

What worked for me, was to create a new UIBarButtonItem without any style. Then set the title and color propertys and set it as backBarButtonItem.

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];
backButton.title = @"go back - now!";
backButton.tintColor = [UIColor colorWithRed:0.1 green:0.5 blue:0.7 alpha:1.0];
self.navigationItem.backBarButtonItem = backButton;
[okButton release];
svn
  • 142
  • 2
  • 9
3

If you want to make the button look exactly like in your picture, you may use an image, too:

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"back_button_bg"]
                                        forState:UIControlStateNormal
                                      barMetrics:UIBarMetricsDefault];

The background image must be a resizable image for good results.

Andreas Ley
  • 9,109
  • 1
  • 47
  • 57
0

Best way I found to set it globally or locally is

    [[UIBarItem appearance] setTitleTextAttributes:
         [NSDictionary dictionaryWithObjectsAndKeys:
         [UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0], UITextAttributeTextColor, 
         [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0], UITextAttributeTextShadowColor, 
         [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset, 
         [UIFont fontWithName:@"AmericanTypewriter" size:0.0], UITextAttributeFont, 
      nil] 
        forState:UIControlStateNormal];
Zeev Vax
  • 914
  • 7
  • 13
-1
[[UINavigationBar appearance]setTintColor:[UIColor whiteColor]];

try this It is working for me...

AlexVogel
  • 10,601
  • 10
  • 61
  • 71
Virus
  • 390
  • 3
  • 6