Change the color of a bar button in iOS 7

2020-07-30 02:43发布

I have the following code in my viewDidLoad of my containing view:

// Now add the next button
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(self)];
self.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor];
self.navigationItem.rightBarButtonItem = nextButton;

The UINavigationController parent has this in viewDidLoad:

[super viewDidLoad];
// Do any additional setup after loading the view.

self.navigationBar.tintColor = [UIColor whiteColor];
self.navigationBar.barTintColor = [UIColor blackColor];

How do I just customize the rightBar button to change the background color and maybe the text color?

4条回答
可以哭但决不认输i
2楼-- · 2020-07-30 02:58

working code.. try this.

UIImage *image = [UIImage imageNamed:@"Image.png"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
self.navigationItem.leftBarButtonItem = menuButton;
查看更多
We Are One
3楼-- · 2020-07-30 03:00

You have to set the tintColor property on the button after you assign the button.

self.navigationItem.rightBarButtonItem = nextButton;
self.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor];

instead of

self.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor];
self.navigationItem.rightBarButtonItem = nextButton;
查看更多
啃猪蹄的小仙女
4楼-- · 2020-07-30 03:00

Try below code you can set color of button as well text color

UIView *customBarButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];

UIButton *buttonDone = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonDone addTarget:self action:@selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

buttonDone.backgroundColor = [UIColor redColor];
   // or try this one
// buttonDone.tintColor = [UIColor redColor];

buttonDone.frame = CGRectMake(10, 0, 50, 30);

buttonDone.titleLabel.textColor = [UIColor colorWithRed:141.0/255.0 green:209.0/255.0 blue:205.0/255.0 alpha:1.0];
buttonDone.titleLabel.font = [UIFont fontWithName:@"SourceSansPro-Light" size:20.0f];
[buttonDone setTitle:@"Done" forState:UIControlStateNormal];
[customBarButtonView addSubview:buttonDone];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:customBarButtonView];
查看更多
倾城 Initia
5楼-- · 2020-07-30 03:10

If you wish to have the same color for all bar buttons you can use the following line of code:

self.window.tintColor = [UIColor redColor];

in your app delegate.

查看更多
登录 后发表回答