or *_*ran 1 user-interface objective-c uinavigationbar ios ios7
我UINAvigatoinBar用十六进制颜色设置我的颜色:
self.navigationController.navigationBar.barTintColor = UIColorFromRGB(0x212121);
Run Code Online (Sandbox Code Playgroud)
它运行良好,IOS7但在较低版本中崩溃与以下:
[UINavigationBar setBarTintColor:]: unrecognized selector sent to instance
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
Run Code Online (Sandbox Code Playgroud)
最好的解决方案是检测哪个版本的操作系统是:
-(void)viewWillAppear:(BOOL)animated {
NSString *ver = [[UIDevice currentDevice] systemVersion];
int ver_int = [ver intValue];
if (ver_int < 7) {
[self.navigationController.navigationBar setTintColor:[UIColor UIColorFromRGB(0x212121)]];
}
else {
self.navigationController.navigationBar.barTintColor = [UIColor UIColorFromRGB(0x212121)];
}
}
Run Code Online (Sandbox Code Playgroud)
我假设最好的方法是使用respondToSelector方法而不是检查iOS版本:
if ([self.navigationController.navigationBar respondsToSelector:@selector(setBarTintColor:)]) {
[self.navigationController.navigationBar setBarTintColor:NAVBAR_BACKGROUNDCOLOR];
}
else {
[self.navigationController.navigationBar setTintColor:NAVBAR_BACKGROUNDCOLOR];
}
Run Code Online (Sandbox Code Playgroud)