【问题标题】:how to set navigation bar color on IOS 6/7如何在 IOS 6/7 上设置导航栏颜色
【发布时间】:2013-12-14 06:15:30
【问题描述】:

我正在用十六进制颜色设置我的UINAvigatoinBar 颜色:

self.navigationController.navigationBar.barTintColor = UIColorFromRGB(0x212121);

它在 IOS7 上运行良好,但在较低版本中它会崩溃并出现以下情况:

[UINavigationBar setBarTintColor:]: unrecognized selector sent to instance

我怎样才能做到正确?

【问题讨论】:

标签: ios objective-c user-interface ios7 uinavigationbar


【解决方案1】:
[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];

【讨论】:

  • 它很好,但是使用外观它会改变导航栏的颜色,而不是针对特定视图的导航栏。
【解决方案2】:

我认为最好的方法是使用 respondToSelector 方法而不是检查 iOS 版本:

if ([self.navigationController.navigationBar respondsToSelector:@selector(setBarTintColor:)]) {
    [self.navigationController.navigationBar setBarTintColor:NAVBAR_BACKGROUNDCOLOR];
}
else {
    [self.navigationController.navigationBar setTintColor:NAVBAR_BACKGROUNDCOLOR];
}

【讨论】:

    【解决方案3】:

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

    -(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)];
       }
    
     }
    

    【讨论】:

      【解决方案4】:

      您需要检查操作系统版本。如果是IOS7,则可以使用barTintColor。在IOS6中,你可以使用tintColor

      if ([self checkOSVersion] >= 7) {
              [[UINavigationBar appearance] setBarTintColor:[UIColor UIColorFromRGB(0x212121)]];
              [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
          } else {
              [[UINavigationBar appearance] setTintColor:[UIColor UIColorFromRGB(0x212121)]];
          }
      

      将操作系统版本检查方法定义为

      - (int)checkOSVersion {
      
          NSArray *ver = [[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."];
          int osVerson = [[ver objectAtIndex:0] intValue];
          return osVerson;
      }
      

      【讨论】:

        【解决方案5】:
        [self.navigationController.navigationBar setTintColor:[UIColor redColor]];
        

        【讨论】:

          【解决方案6】:
          self.navigationController.navigationBar.tintColor=[UIColor colorWithRed:(45/255.f) green:(45/255.f) blue:(45/255.f) alpha:1.0f];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-08-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-09-26
            • 1970-01-01
            • 2013-10-07
            • 1970-01-01
            相关资源
            最近更新 更多