Change UIBarbuttonItems Color

放肆的年华 提交于 2020-01-21 06:28:09

问题


How can I set UIBarButtonItem's color to green? I'm using iOS 4, there is no tint property. please help me.


回答1:


In iOS 4:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"green.png"] forState:UIControlStateNormal];
button.frame=CGRectMake(0.0, 100.0, 60.0, 30.0);
[button setTitle:@"Green" forState:UIControlStateNormal];
[button addTarget:self action:@selector(yourAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithCustomView:button];

Here you need a green image for doing this, you are creating a custom button with this image and set it as the view of UIBarButtonItem.

In iOS 5 you can use:

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

Please check these links for more:

  1. UIAppearance Protocol
  2. User interface customization



回答2:


Please try the below code.

self.navigationController.navigationBar.tintColor = [UIColor greenColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:NULL];



回答3:


I know this is an old question, but in case anyone is using a customView, you can try:

myButton.customView.tintColor = [UIColor blackColor];



回答4:


In Swift 4

This is how you can add your custom color to UIBarButtonItem

let barButtonItem = UIBarButtonItem.init(
        title: "Paired",
        style: .done,
        target: self,
        action: #selector(self.myAction(sender:))
      )
barButtonItem.tintColor = UIColor.init(red: 247/255, green: 65/255, blue: 126/255, alpha: 1.0)
self.navigationItem.rightBarButtonItem = barButtonItem


来源:https://stackoverflow.com/questions/13677703/change-uibarbuttonitems-color

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