Alex1987
Alex1987

Reputation: 9457

iOS: changing the tint color of the back button?

I have a navigation controller that pushes a UIViewController. I would like to change the tint color of the back button of the navigation item when a user presses on a certain button. Is this possible? I have tried using [UIBarButtonItem appearance] setTintColor: but it only works on initialization (for example in viewDidLoad) and not otherwise.

Upvotes: 5

Views: 7248

Answers (5)

Clint Warner
Clint Warner

Reputation: 1265

In the method that is called on your button click, just put [self.navigationItem.backBarButtonItem setTintColor:[UIColor *whateverColorYouWant*]]; I haven't tested it but I'm 99% sure that would work

Edit: Just tested it, it works.

Upvotes: 3

EeKay
EeKay

Reputation: 6770

The easiest thing for me was to set the tint color for ALL uibarbutton items:

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

And then explicitly setting the tintcolor for explicit navigationbuttons that I create & place on the navigationbar to other colors...

Saves me the headache of creating custom backbuttons when all I want to do is change the tint.

Upvotes: 7

Satheesh
Satheesh

Reputation: 11276

Try this:

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class],[UIToolbar class], nil] setTintColor:[UIColor redColor]];

Upvotes: 0

Rudolf Adamkovič
Rudolf Adamkovič

Reputation: 31486

Here's a code that changes text and color of the back button:

- (void)viewDidLoad
{
    UIBarButtonItem *backButton = [UIBarButtonItem new];
    [backButton setTitle:@"Back"];
    [backButton setTintColor:[UIColor yellowColor]];
    [[self navigationItem] setBackBarButtonItem:backButton];
}

Upvotes: 0

Wolverine
Wolverine

Reputation: 4329

Try this......

You need to use a UIBarButtonItem with a custom view. Something like this:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
[button addTarget:target action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:@"back_button.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"back_button_tap.png"] forState:UIControlStateHighlighted];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

And put the button to the navigation bar, usually in a controller with UINavigationController:

self.navigationItem.leftBarButtonItem = buttonItem;

Upvotes: 8

Related Questions