Question

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

Was it helpful?

Solution

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)

OTHER TIPS

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];

This worked for me.

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

It is very similar to the second answer...

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.

Swift way:

It changes all items in the nav.

In AppDelegate do something like this:

let navControl = UINavigationBar.appearance()
    navControl.tintColor = UIColor.grayColor()

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!!

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

    [navigationBar setBarStyle: UIBarStyleBlack];

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

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

What worked for me was this :

self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor textPrimaryColor] forKey:NSForegroundColorAttributeName];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top