我如何改变导航栏的颜色在iOS 7?
基本上我想要实现类似Twitter导航栏的东西(更新的Twitter为iOS7)。我在视图控制器的顶部嵌入了一个导航条。所有我想要的是改变导航栏的颜色为浅蓝色连同顶部的工具栏。我似乎在我的故事板中找不到一个选项。
我如何改变导航栏的颜色在iOS 7?
基本上我想要实现类似Twitter导航栏的东西(更新的Twitter为iOS7)。我在视图控制器的顶部嵌入了一个导航条。所有我想要的是改变导航栏的颜色为浅蓝色连同顶部的工具栏。我似乎在我的故事板中找不到一个选项。
当前回答
在一个基于导航的应用程序中,你可以把代码放在AppDelegate中。更详细的代码可以是:
// Navigation bar appearance (background and title)
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor titleColor], NSForegroundColorAttributeName, [UIFont fontWithName:@"FontNAme" size:titleSize], NSFontAttributeName, nil]];
[[UINavigationBar appearance] setTintColor:[UIColor barColor]];
// Navigation bar buttons appearance
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor textBarColor], NSForegroundColorAttributeName, shadowColor, NSShadowAttributeName, [UIFont fontWithName:@"FontName" size:titleSize], NSFontAttributeName, nil];
其他回答
如果你需要支持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];
}
}
颜色:
[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
的图像
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar_320X44.png"] forBarMetrics:UIBarMetricsDefault];
在viewDidLoad中,设置:
self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
更改(blueColor)为任何你想要的颜色。
这个问题和这些答案很有帮助。有了它们,我可以设置我想要的深蓝色导航栏颜色,白色标题和按钮文本。
但我还需要将时钟、载波、信号强度等更改为白色。黑色和深蓝色的对比不够明显。
我可能在之前的回答中忽略了这个解决方案,但我能够通过在我的顶级viewController的viewDidLoad中添加这一行来进行更改:
[self.navigationController.navigationBar setBarStyle:UIStatusBarStyleLightContent];
如果你想使用十六进制代码,这里是最好的方法。
首先,在类的顶部定义它:
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
然后在“application didFinishLaunchingWithOptions”里面,放这个:
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x00b0f0)];
把十六进制代码放在00b0f0的位置。