简体   繁体   中英

Change UIBarbuttonItems Color

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

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

Please try the below code.

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

我知道这是一个老问题,但是如果有人使用customView ,你可以尝试:

myButton.customView.tintColor = [UIColor blackColor];

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM