Setting Bar Button Item color in a custom navigati

2019-01-24 23:58发布

问题:

I created a custom navigation bar and a right navigation button using the XIB. This works fine. But I need to to customize the tint color of the right navigation button. At the moment this tint color is the same color as tint color of navigation bar. I need a different color for this right button. Is there any way to change this color?

Thank you.

回答1:

Not to my knowledge, the button inherits the tint color of the navigationBar. What you can do is set a customView for the navigationItem:

This is how you set it with one of the SDK's buttons:

UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareButtonHandler:)];

[self.navigationItem setRightBarButtonItem:shareButton];
[shareButton release];

Instead you can do it like this:

UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"yourImage.png" style:UIBarButtonItemStyleBordered target:self action:@selector(customButtonHandler:)]];

To use an image you made in photoshop etc.

There is als an initWithCustomView:UIView or initWithTitle:NSString you can use.

Sorry no "one-line solution" :)



回答2:

In iOS 5 there is 'appearance' feature:

[[UIBarButtonItem appearance] setTintColor:YOUR_COLOR];



回答3:

Swift 3.x

UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : Color.primaryActionColor], for: .normal)