18

I am trying to set the tint color of the back button within a navigation controller, but nothing is working. I have tried

[self.navigationController.backBarButtonItem setTintColor:myColor];
//myColor was previously set

But it stays the default tint

iDev
  • 23,310
  • 7
  • 60
  • 85
user717452
  • 33
  • 14
  • 73
  • 149

10 Answers10

38

I believe barButtonItem is read-only. (I'm not too sure about this, someone correct me if I'm wrong)

To give a tint colour to these buttons, this is what I would do:

In your App Delegate, add these lines of code:

UIBarButtonItem *barButtonAppearance = [UIBarButtonItem appearance];
[barButtonAppearance setTintColor:[UIColor redColor]]; // Change to your colour

The first line sets an appearance proxy, so I believe this works too, if you like less code:

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

I hope this works for you! (This changes all instances of UIBarButtonItem)

Qiming
  • 1,664
  • 1
  • 14
  • 18
  • 2
    That's what I had been doing, but the same view controller can pull up a Mail Composer View Controller, and it changes the colors of the buttons there, which goes against the no customization rules for that controller. – user717452 Jul 20 '12 at 18:10
  • For my bar multiple buttons - 2 right buttons and 2 left buttons. Inner buttons was with default (blue) tint colour. navItem.rightBarButtonItems = @[exButton, fwButton]; fwButton was blue, exButton was red. After adding of self.window.tintColor = [UIColor colorWithRed:222.0/255.0 green:33.0/255.0 blue:0.0/255.0 alpha:1.0]; in the AppDelegate all buttons are with this new red colour. – Panayot Aug 31 '18 at 06:26
32

You can't change the tint color of UINavigationItem directly. You have to change the tint color of navigation bar and your bar button will automatically change its color.

Add these lines in your viewWillAppear method

[[self.navigationController navigationBar] tintColor] = [UIColor myColor];

Or You can create a custom button and initialize your UIBarButtonItem as

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:yourUIButton];
Chris Villa
  • 3,901
  • 1
  • 18
  • 10
  • 2
    Yeeeeep! Working with iOS 7, Thanks! :D – VICTORGS3 Jan 19 '14 at 00:28
  • I agree, this is the accepted answer. Also, if you want to apply the color for all parts of the app, just add this to AppDelegate.didFinishLaunchingWithOptions: `UINavigationBar.appearance().tintColor = UIColor.redColor()` – netwire Aug 20 '14 at 03:33
25

This worked for me.

[self.navigationController.navigationBar setTintColor:[UIColor redColor]];

It is very similar to the second answer...

Eric
  • 251
  • 3
  • 2
4

If you prefer to do it in Interface Builder without writing code:

  1. Select your UINavigationBar (inside UINavigationController)

enter image description here

  1. In Attributes Inspector, find "Tint" - this is what sets Back Button color. Note that "Bar Tint" is not the same - this sets Navigation Bar background color.

enter image description here

It is important to set Back Button appearance this way, and not try to implement leftBarButtonItem, because implementing leftBarButtonItem will disable the built-in back navigation gestures, like swipe to go back.

Alex
  • 1,574
  • 17
  • 36
3

Swift way:

It changes all items in the nav.

In AppDelegate do something like this:

let navControl = UINavigationBar.appearance()
    navControl.tintColor = UIColor.grayColor()
Joseph Geraghty
  • 1,827
  • 1
  • 10
  • 7
0

Well you can certainly change the color of just the Back Button or perhaps any Bar Button on the Navigation bar. Here's a small snippet to do it.

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:nil];
backButton.tintColor = [UIColor blackColor];
[self.navigationItem setBackBarButtonItem:backButton];
[backButton release];

Well this is just one of the ways for doing this. I hope this helps!! I know the answer for it is already accepted, but I thought to give a proper snippet to it. Cheers!!! Happy Coding!!

Apple_iOS0304
  • 1,092
  • 1
  • 9
  • 19
0

If you want to set it to predefined style you can use

    [navigationBar setBarStyle: UIBarStyleBlack];
r0ro
  • 196
  • 2
  • 8
0

Place the following code in AppDelegate that will set color for Back button globally

//Set color of BackButtonItem globally

[[UIBarButtonItem appearance] setTintColor:[UIColor colorWithRed:5.0/255.0 
green:127.0/255.0 blue:173.0/255.0 alpha:1.0]];

This is supported in iOS 5 and above

Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
0

It always work for me:

  1. self.navigationItem.leftBarButtonItem = [self logicToAddBackButton];

  2. GET DEFAULT BACK BUTTON

-

(UIBarButtonItem*) logicToAddBackButton
        {
            UIImageView *imageView;

            // [imageView setTintColor:[UIColor redColor]];
            UILabel *label=[[UILabel alloc] init];
            if (WHITEBACK) {
                imageView  =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UiNavigationBackIPAD"]];
                [label setTextColor:[UIColor whiteColor]];
            }else{ //DEFAULTBACK
                imageView  =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UiNavigationBack"]];
                [label setTextColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]];

            }

            [label setText:@"Back"];
            [label sizeToFit];

            int space=6;
            label.frame=CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+space,
    label.frame.origin.y, label.frame.size.width,
    label.frame.size.height);
            UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, label.frame.size.width+imageView.frame.size.width+space,
    imageView.frame.size.height)];

            view.bounds=CGRectMake(view.bounds.origin.x+8, view.bounds.origin.y-1, view.bounds.size.width,
    view.bounds.size.height);


            UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(view.bounds.origin.x, view.bounds.origin.y,
    view.bounds.size.width, view.bounds.size.height)];
            button.bounds=CGRectMake(view.bounds.origin.x, view.bounds.origin.y, view.bounds.size.width,
    view.bounds.size.height);
            [button addTarget:self action:@selector(eventBack) forControlEvents:UIControlEventTouchUpInside];
            [button addSubview:imageView];
            [button addSubview:label];


            [UIView animateWithDuration:0.33 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
                label.alpha = 0.0;
                CGRect orig=label.frame;
                label.frame=CGRectMake(label.frame.origin.x+25, label.frame.origin.y -5, label.frame.size.width,
    label.frame.size.height+10);
                label.alpha = 1.0;
                label.frame=orig;
            } completion:nil];

            UIBarButtonItem *backButton =[[UIBarButtonItem alloc] initWithCustomView:button];


            return backButton;
        }
  • BACK BUTTON ACTION

    (void)eventBack { [self.navigationController popViewControllerAnimated:YES]; }

  • UiNavigationBack Image (Please change colour of image as require with same size [25X41 144pixels/inch] to get default look) enter image description here

Yogesh Lolusare
  • 2,162
  • 1
  • 24
  • 35
0

What worked for me was this :

self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor textPrimaryColor] forKey:NSForegroundColorAttributeName];
Nilesh Agrawal
  • 3,002
  • 10
  • 26
  • 54