问题
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?
回答1:
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;
回答2:
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.
回答3:
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];
回答4:
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;
来源:https://stackoverflow.com/questions/19088428/change-the-color-of-a-bar-button-in-ios-7