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

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


当前回答

它实际上比我在这里看到的答案更简单:

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).

我希望这能帮助到一些人。我不喜欢我看到的答案。我喜欢让我的代码尽可能的干净。并不是说用编程的方式做这件事是错误的,但确实有像我这样的人....这是给你们的。

其他回答

颜色:

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

的图像

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

为了使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];
}

在ios7中,你必须使用-barTintColor属性:

navController.navigationBar.barTintColor = [UIColor barColor];

如果你想使用十六进制代码,这里是最好的方法。

首先,在类的顶部定义它:

#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的位置。

快速改变导航条颜色:

self.navigationController?.navigationBar.barTintColor = UIColor.red

更改标题字体、大小、颜色:

self.title = "title"
self.navigationController?.navigationBar.titleTextAttributes = [
        NSAttributedString.Key.foregroundColor : UIColor.white,
        NSAttributedString.Key.font : UIFont(name: "Futura", size: 30)!
    ]