如何在IOS 6/7上设置导航栏颜色

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)

我该怎么办?

tho*_*b65 7

[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
Run Code Online (Sandbox Code Playgroud)


Ila*_*rio 5

最好的解决方案是检测哪个版本的操作系统是:

-(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)


Lis*_*ien 5

我假设最好的方法是使用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)