Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 857
Alex1987 Avatar asked Dec 03 '22 02:12

Alex1987


2 Answers

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;
like image 85
Wolverine Avatar answered Dec 18 '22 17:12

Wolverine


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.

like image 33
EeKay Avatar answered Dec 18 '22 17:12

EeKay