UIBarButtonItem setTintColor doesn't work on iOS7

我们两清 提交于 2019-12-20 01:41:39

问题


In iOS6, I used this code to make my UIBarButtonItem:

UIBarButtonItem* validate = [[UIBarButtonItem alloc]initWithTitle:@"MyTitle" style:UIBarButtonItemStylePlain target:self action:@selector(actionValidate)];
    [validate setTintColor:[UIColor orangeColor]];
    self.navigationItem.rightBarButtonItem = validate;

It works fine in iOS6 but in iOS7, the color of the button changes only when you push it.. How can I fix this?


回答1:


In iOS7 you if you need to change the navigationBar buttons color, you must set tintColor for the navgationBar not the for specific barButton any more.

navigationController.navigationBar.tintColor = [UIColor orangeColor];

Edit: this works in iOS7, you need to do the check:

float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersion >= 7.0)
{
    navigationController.navigationBar.tintColor = [UIColor orangeColor]
}



回答2:


I couldn't get @Chris's method to work (iOS 8 like @Adama says).

My use case is that I want to set all UIToolbar & UINavigationBar buttons to a default colour. So using the UIAppearance API:

UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.redColor()], forState: .Normal)



来源:https://stackoverflow.com/questions/19131481/uibarbuttonitem-settintcolor-doesnt-work-on-ios7

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