2013-12-11 32 views
0

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

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

但上面的代码并未将导航栏的颜色更改为红色。我错过了什么。你能帮我么。由于

+0

在您添加代码的委托文件或?? – Yohan

+0

确保你的'self.navigationController'不应该是零。 –

+0

并尝试'if([[[UIDevice currentDevice] systemVersion] floatValue]> = 7)'而不是使用数组。 –

回答

3

将下面的代码在didFinishLaunchingWithOptions的AppDelegate

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; 
} 
+0

非常感谢manujmv。这工作。 –

+0

欢迎。快乐编码:) – manujmv

1

为了增加颜色的所有导航栏默认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

+0

非常感谢Yohan。这工作。 –

+0

高兴地帮助你 – Yohan

0

我会说这是失败的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,因为它会追加0在结束时,如果它只是返回7,所以我们希望它永远只给我们的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 
}