【问题标题】:Unable to set the colour in UINavigation bar - iOS无法在 UINavigation 栏中设置颜色 - iOS
【发布时间】:2013-12-11 11:47:14
【问题描述】:

我正在尝试为 iPhone 应用程序中的 UINavigation 栏设置不同的颜色。我的应用程序在 iOS 7 中运行,我使用的代码 sn-p 如下

NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.navigationController.navigationBar.translucent = NO;
}else {
    self.navigationController.navigationBar.tintColor = [UIColor redColor];
}

但上面的代码并没有将导航栏的颜色更改为红色。我是不是错过了什么。你能帮我么。谢谢

【问题讨论】:

  • 你在哪里添加代码委托文件??
  • 确保您的 self.navigationController 不应该为 nil。
  • 并尝试使用if([[[UIDevice currentDevice] systemVersion] floatValue]>=7) 代替数组。

标签: iphone objective-c ios7 uinavigationbar


【解决方案1】:

将以下代码放在 AppDelegate

didFinishLaunchingWithOptions
if ([self checkOSVersion] >= 7) {
    [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
    [[UINavigationBar appearance] setTranslucent:NO];
} else {
    [[UINavigationBar appearance] setTintColor:[UIColor redColor]];
}

- (int)checkOSVersion {

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

【讨论】:

    【解决方案2】:

    为 appdelegate.m 中代码下方的所有导航栏默认设置添加颜色

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Override point for customization after application launch.
        [[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];
        return YES;
    }
    

    或者想为特定视图设置颜色,然后使用这个

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
       self.navigationController.navigationBar.barTintColor = [UIColor grayColor];
    
    }
    

    如果您想了解更多信息,请尝试此链接check this link

    【讨论】:

      【解决方案3】:

      我会说它没有通过 if 语句 if ([[ver objectAtIndex:0] intValue] >= 7)。我会将整个 if statement 替换为:

      if([[[UIDevice currentDevice] systemVersion] intValue] >= 7) {
          // Do the below if you want to set the bar tint color for every UINavigationBar
          // [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
          // [[UINavigationBar appearance] setTranslucent:NO]; 
          [[[self navigationController] navigationBar] setBarTintColor:[UIColor redColor]];
          [[[self navigationController] navigationBar] setTranslucent:NO];
      
       } else {
           // Do the below if you want to set the bar tint color for every UINavigationBar
           // [[UINavigationBar appearance] setTintColor:[UIColor redColor]];
           [[[self navigationController] navigationBar] setTintColor:[UIColor redColor]];
       }
      

      [[UIDevice currentDevice] systemVersion]; 将返回一个NSString *,它可以是从77.0.1 的任何值。执行floatValue 将至少返回7.0,因为如果它只返回7,它将在末尾附加0,所以我们希望它始终只给我们7 的初始系统版本,所以我们需要最后做intValue,这样看起来[[[UIDevice currentDevice] systemVersion] intValue];就忘了componentSeperatedByString:方法,根本不需要它,它会导致比你想要的更多的问题。

      你也可以这样做if statement

      if (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_7_0) {
          // Everything above and including iOS 7.0
      } else {
          // Everything below iOS 7.0
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-20
        • 2014-08-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多