我如何改变导航栏的颜色在iOS 7?
基本上我想要实现类似Twitter导航栏的东西(更新的Twitter为iOS7)。我在视图控制器的顶部嵌入了一个导航条。所有我想要的是改变导航栏的颜色为浅蓝色连同顶部的工具栏。我似乎在我的故事板中找不到一个选项。
我如何改变导航栏的颜色在iOS 7?
基本上我想要实现类似Twitter导航栏的东西(更新的Twitter为iOS7)。我在视图控制器的顶部嵌入了一个导航条。所有我想要的是改变导航栏的颜色为浅蓝色连同顶部的工具栏。我似乎在我的故事板中找不到一个选项。
当前回答
在基于导航的应用程序中,您可以更改颜色
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:19.0/255.0 green:86.0/255.0 blue:138.0/255.0 alpha:1];
self.navigationController.navigationBar.translucent = NO;
} else {
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:19.0/255.0 green:86.0/255.0 blue:138.0/255.0 alpha:1];
}
其他回答
在viewDidLoad中,设置:
self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
更改(blueColor)为任何你想要的颜色。
它实际上比我在这里看到的答案更简单:
1) Just make sure you select the navigation bar on the Navigation control.
2) Select the color you want in the bar tint.
3) You have other options too, and/or individually on each view (just play with it).
我希望这能帮助到一些人。我不喜欢我看到的答案。我喜欢让我的代码尽可能的干净。并不是说用编程的方式做这件事是错误的,但确实有像我这样的人....这是给你们的。
tintColor对条形图的行为在ios7.0中发生了改变。它不再影响酒吧的背景。
从文档中可以看到:
barTintColor类引用
要应用于导航栏背景的色调。
@property(nonatomic, retain) UIColor *barTintColor
讨论 默认情况下,这种颜色是半透明的,除非你将半透明属性设置为NO。
可用性
支持iOS 7.0及以上版本。
中声明 UINavigationBar.h
Code
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
// iOS 7.0 or later
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.navigationController.navigationBar.translucent = NO;
}else {
// iOS 6.1 or earlier
self.navigationController.navigationBar.tintColor = [UIColor redColor];
}
我们也可以使用这个来检查iOS版本,正如iOS 7 UI过渡指南中提到的那样
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// iOS 6.1 or earlier
self.navigationController.navigationBar.tintColor = [UIColor redColor];
} else {
// iOS 7.0 or later
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.navigationController.navigationBar.translucent = NO;
}
编辑 使用xib
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
}
如果你需要支持ios6和ios7那么你在你的UIViewController中使用这个得到那个特别的浅蓝色:
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:89/255.0f green:174/255.0f blue:235/255.0f alpha:1.0f];
self.navigationController.navigationBar.translucent = NO;
}else{
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:89/255.0f green:174/255.0f blue:235/255.0f alpha:1.0f];
}
}