iPhone Set Tint Color of Back Bar Button Item

2019-01-17 16:39发布

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

10条回答
冷血范
2楼-- · 2019-01-17 17:10

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];
查看更多
The star\"
3楼-- · 2019-01-17 17:12

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

查看更多
Summer. ? 凉城
4楼-- · 2019-01-17 17:15

This worked for me.

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

It is very similar to the second answer...

查看更多
Lonely孤独者°
5楼-- · 2019-01-17 17:15

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

查看更多
登录 后发表回答