如何从AppDelegate更改UINavigationBar背景颜色

我知道如何通过做更改UINavigationBar背景图像

 [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nabbar"] forBarMetrics:UIBarMetricsDefault]; 

而且我知道如何在每个Views中将栏设置为不同的颜色…..现在,我想更改背景颜色, 而不使用 app delegate的纯色图像 。 我不想每次从每个视图设置它,我不想写一个CGRect

我试过[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:33/255.0 green:34/255.0 blue:36/255.0 alpha:1.0]]; 但我不工作,我无法find代码中的任何地方的代码。

任何人都可以请指出我在正确的方向吗?

你可以使用[[UINavigationBar appearance] setTintColor:myColor];

从iOS 7开始,你需要设置[[UINavigationBar appearance] setBarTintColor:myColor]; 还有[[UINavigationBar appearance] setTranslucent:NO]

 [[UINavigationBar appearance] setBarTintColor:myColor]; [[UINavigationBar appearance] setTranslucent:NO]; 

要更改背景颜色而不是色调,以下代码将起作用:

 [self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]]; [self.navigationController.navigationBar setTranslucent:NO]; 

为了在iOS 7中做到这一点:

 [[UINavigationBar appearance] setBarTintColor:myColor]; 

Swift语法:

  UINavigationBar.appearance().barTintColor = UIColor.whiteColor() //changes the Bar Tint Color 

我只是把它放在AppDelegate的didFinishLaunchingWithOptions,它贯穿整个应用程序

你可以用Xcode 6.3.1轻松做到这一点。 在文档大纲中select您的导航栏。 select属性检查器。 取消半透明。 将Bar Tint设置为所需的颜色。 完成!

正如其他答案中提到的,你可以使用setTintColor:但是你想要一个纯色,这是不可能的设置色彩颜色AFAIK。

解决方法是以编程方式创build图像,并通过UIAppearance图像设置为所有导航栏的背景图像。 关于图像的大小,我不确定1×1像素的图像是否可以工作,或者是否需要导航栏的确切大小。请检查此问题的第二个答案,以了解如何创build图像。

作为一个build议,我不喜欢用这些types的东西“重载”应用程序委托。 我倾向于创build一个名为AppearanceConfiguration的类,只有一个公共方法configureAppearance ,我设置所有UIAppearance的东西,我想要的,然后我从应用程序委托调用该方法。

您可以在任何视图控制器中使用此代码来设置UINavigation背景颜色

 self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:10.0f/255.0f green:30.0f/255.0f blue:200.0f/255.0f alpha:1.0f]; 

你想设置tintColor

颜色代码是这里的问题。 而不是使用195/255,使用0.7647或195.f / 255.f问题是转换浮动不正常。 尝试使用精确的浮点值。