我如何改变导航栏的颜色在iOS 7?

基本上我想要实现类似Twitter导航栏的东西(更新的Twitter为iOS7)。我在视图控制器的顶部嵌入了一个导航条。所有我想要的是改变导航栏的颜色为浅蓝色连同顶部的工具栏。我似乎在我的故事板中找不到一个选项。


当前回答

self.navigationBar.barTintColor = [UIColor blueColor];
self.navigationBar.tintColor = [UIColor whiteColor];
self.navigationBar.translucent = NO;

// *barTintColor* sets the background color
// *tintColor* sets the button's color

其他回答

在一个基于导航的应用程序中,你可以把代码放在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];

颜色:

[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];

的图像

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar_320X44.png"] forBarMetrics:UIBarMetricsDefault];

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

这个问题和这些答案很有帮助。有了它们,我可以设置我想要的深蓝色导航栏颜色,白色标题和按钮文本。

但我还需要将时钟、载波、信号强度等更改为白色。黑色和深蓝色的对比不够明显。

我可能在之前的回答中忽略了这个解决方案,但我能够通过在我的顶级viewController的viewDidLoad中添加这一行来进行更改:

[self.navigationController.navigationBar setBarStyle:UIStatusBarStyleLightContent];

为了使Rajneesh071的代码完整,你可能还想设置导航栏的标题颜色(和字体,如果你想要的话),因为默认行为从iOS 6改变到7:

NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7)
{
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.translucent = NO;
    NSMutableDictionary *textAttributes = [[NSMutableDictionary alloc] initWithDictionary:mainNavController.navigationBar.titleTextAttributes];
    [textAttributes setValue:[UIColor whiteColor] forKey:UITextAttributeTextColor];
    self.navigationController.navigationBar.titleTextAttributes = textAttributes;
}
else
{
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
}