如何在iOS 7或6中更改导航栏颜色?

64
我想要更改导航栏的颜色,但我不确定是否应该更改色调或背景。我知道iOS 7正在朝着更加扁平化的设计方向发展(甚至建议去除渐变效果),但我很难分辨这两者之间的区别。即使我设置了背景颜色,也没有任何变化。
在这张图片中,背景设为绿色,但条形区域仍然是蓝色的: 输入图像描述

https://dev59.com/n2Mk5IYBdhLWcg3w0hM-#18929980 - Rajneesh071
16个回答

107

iOS 7.0 中,tintColor 对于 bar 的行为发生了变化。它不再影响 bar 的背景,并且与添加到 UIView 的 tintColor 属性的行为相同。 要对 bar 的背景进行色调处理,请使用 -barTintColor。

navController.navigationBar.barTintColor = [UIColor navigationColor];


4
确认,在iOS 7中tintColor无效,但barTintColor有效。 - Pieter Gunst
4
你或许也想设置 navController.navigationBar.translucent = NO。这会使导航栏不透明。 - Vladimir Grigorov
1
在这里查看完整答案:https://dev59.com/n2Mk5IYBdhLWcg3w0hM- - OhadM

79

如果你想在iOS 6中像iOS 7一样为导航栏设置纯色,请使用以下代码:

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor greenColor]];

iOS 7中,可以像这样使用barTintColor


navigationController.navigationBar.barTintColor = [UIColor greenColor];
或者
 [[UINavigationBar appearance] setBarTintColor:[UIColor greenColor]];

我正在使用自定义导航栏,而上述代码在我的设备上(iOS7)无法工作。 我必须明确地编写以下代码: [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}]; - Vladimír Slavík
[[UINavigationBar appearance] setBarTintColor:[UIColor greenColor]]; 应该在iOS 7中正常工作。 - Carmen

36

// 在iOS 7中:

[self.navigationController.navigationBar setBarTintColor:[UIColor yellowColor]];

// 在iOS 6中:-

[self.navigationController.navigationBar setTintColor:[UIColor yellowColor]];

谢谢,它正在工作,但如何更改特定视图控制器导航栏颜色,请帮助我。 - vijay

11

UINavigationBar上忽略背景颜色属性,所以如果你想调整外观和感觉,你要使用tintColor或者调用其他列在UINavigationBar类参考的"自定义栏外观"下面的方法(例如 setBackgroundImage:forBarMetrics:)。

请注意,在iOS 7中tintColor属性的工作方式不同,因此,如果您希望在iOS 7和之前的版本之间获得一致的外观,最好使用背景图像。另外值得一提的是,您无法在Storyboard中配置背景图像,您需要创建一个IBOutlet到您的UINavigationBar并在viewDidLoad或其他适当的位置更改它。


5

还有一件事,如果你想在UIPopover中更改导航栏背景颜色,你需要将barStyle设置为UIBarStyleBlack

if([UINavigationBar instancesRespondToSelector:@selector(barTintColor)]){ //iOS7
    navigationController.navigationBar.barStyle = UIBarStyleBlack;
    navigationController.navigationBar.barTintColor = [UIColor redColor];
}

4
您可以检查iOS版本并轻松设置导航栏的色调。
if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.9529 green:0.4392 blue:0.3333 alpha:1.0];
}else{

    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.9529 green:0.4392 blue:0.3333 alpha:1.0];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

4
以下是如何在iOS 6和7上正确设置它的方法。
+ (void)fixNavBarColor:(UINavigationBar*)bar {
    if (iosVersion >= 7) {
        bar.barTintColor = [UIColor redColor];
        bar.translucent = NO;
    }else {
        bar.tintColor = [UIColor redColor];
        bar.opaque = YES;
    }
}

3
不应检查 iosVersion,而应该使用 respondsToSelector,即 [[UINavigationBar appearance] respondsToSelector:@selector(barTintColor)] - smitt04
2
对于代理,您应该使用instancesRespondToSelector。在这种情况下,它将是[UINavigationBar instancesRespondToSelector:@selector(barTintColor)]。 - James Jones

4
版本检查的完整代码。
 if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {

    // do stuff for iOS 7 and newer
    [self.navigationController.navigationBar setBarTintColor:[UIColor yellowColor]];
}
else {

    // do stuff for older versions than iOS 7
    [self.navigationController.navigationBar setTintColor:[UIColor yellowColor]];
}

3

根据发布的答案,这个对我有用:

/* check for iOS 6 or 7 */
if ([[self navigationController].navigationBar respondsToSelector:@selector(setBarTintColor:)]) {
    [[self navigationController].navigationBar setBarTintColor:[UIColor whiteColor]];

} else {
    /* Set background and foreground */
    [[self navigationController].navigationBar setTintColor:[UIColor whiteColor]];
    [self navigationController].navigationBar.titleTextAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:[UIColor blackColor],UITextAttributeTextColor,nil];
}

2
请将以下代码插入到AppDelegate.m文件中的didFinishLaunchingWithOptions()函数中:
[[UINavigationBar appearance] setBarTintColor:[UIColor
    colorWithRed:26.0/255.0 green:184.0/255.0 blue:110.0/255.0 alpha:1.0]];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接