Setting Bar Button Item color in a custom navigation bar

岁酱吖の 提交于 2019-11-27 22:42:15

问题


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)



回答4:


If you have navigation bar button item with custom view you should convert to UIButton first:

navigationItem.rightBarButtonItem?.customView as? UIButton

Then you can add the attributes of UIButton such as:

button.setTitleColor(UIColor.black.withAlphaComponent(0.1), for: .normal)


来源:https://stackoverflow.com/questions/4518617/setting-bar-button-item-color-in-a-custom-navigation-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!