0
我基本上有一個ViewController,它是UINavigationController的根ViewController。基本上我有一個UIBarButtonItem,當用戶按下它時,它的色彩應該在紅色和綠色之間切換。但顏色似乎沒有改變。我的代碼:iOS:無法更改UIBarButtonItem的顏色
@interface TestButtonColorViewController(){
BOOL colorMode;
}
@end
@implementation TestButtonColorViewController
- (void)viewDidLoad
{
[super viewDidLoad];
colorMode = NO;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Change" style:UIBarButtonItemStyleBordered target:self action:@selector(changeColor)];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)changeColor{
colorMode = !colorMode;
if (colorMode) {
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[UIColor redColor]];
}
else {
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[UIColor greenColor]];
}
}
的UIAppearance方法僅用於色彩控制在創作,而不是事後。 – CSmith