2012-04-24 79 views
8

有没有什么办法可以将UINavigationController的导航栏的背景设置为纯色?将NavigationBar背景设置为纯色

我知道我可以改变色调的颜色,但仍然留下了渐变/玻璃效果。

任何我可以摆脱的方式,只是有一个普通的旧纯色?

回答

5

我认为你必须继承UINavigationBar的并覆盖-(void)drawRect:(CGRect)rect

UIColor *colorFlat = /* any UIColor*/ 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetFillColorWithColor(context, [colorFlat CGColor]); 
CGContextFillRect(context, rect); 
+0

谢谢。奇迹般有效。要设置按钮的颜色,我更改了导航栏的色调颜色,并获得了色调颜色。正如我所料。 – MartinHN 2012-04-24 19:01:34

+0

你知道如果可以通过外观代理来完成吗? '[[UINavigationBar appearance] .....];' – MartinHN 2012-04-24 19:04:07

+0

我不知道,我在iOS 4.2的项目中使用此代码时,外观不可用 – 2012-04-24 19:07:03

-4

创建CustomUIViewController延伸的UIViewController并覆盖viewDidLoad中到这样的事情:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    self.view.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0]; 
} 

之后,用你的CustomUIViewController在控制器中。

Credits

+0

也不是没有可能的形象呢? – MartinHN 2012-04-24 18:19:43

+0

舒尔,抱歉的错误,尝试使用:[UIColor colorWithRed:0.0绿色:0.0蓝色:1.0 alpha:1.0] – kcho0 2012-04-24 18:25:01

+0

没有运气。我不应该在导航栏上设置背景吗?例如'self.navigationController.navigationBar.backgroundColor'?哪个不起作用:) – MartinHN 2012-04-24 18:30:16

46

下面的代码也导致在导航栏的纯色:

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault]; 
[[UINavigationBar appearance] setBackgroundColor:[UIColor redColor]]; 
+2

一定要设置颜色或导航栏上的按钮不会获​​得颜色:[[UINavigationBar appearance] setTintColor:[UIColor redColor]]; – 2013-07-16 04:56:07

+0

你在哪里做这个?我试图在导航栏的视图控制器的viewDidLoad中做到这一点,它似乎不起作用。它是否需要提前一点完成。我有一个故事板似乎是压倒性的。 – David 2015-03-24 14:03:54

+0

大卫,在呈现视图之前必须完成。配置“外观”不会更新可见视图。如果你想在全球范围内设置,最好的地方是应用程序:didFinishLaunchingWithOptions :. – Lubiluk 2015-03-25 14:38:01

0

我用来覆盖drawRect方法和填充颜色;但在iOS7升级之后,它会在UINavigationBar上导致一些问题。如果您编写自己的drawrect方法,即使您调用[super drawRect],它也会更改条​​的尺寸,并最终得到一个44像素高的导航条。状态栏留空。

为了得到纯色的导航栏,我使用的图像作为背景图片(任何小坚实的彩色图像会做,因为你是拉伸它),并添加UINavigationBar的子类的initWithFrame方法这里面线:

[self setBackgroundColor:[UIColor clearColor]]  
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"bgimage.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5) resizingMode:UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault]; 
0

下面的代码为我工作:

[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; 
[self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]]; 
self.navigationController.navigationBar.translucent = NO; 
[self.navigationController.navigationBar setShadowImage:[UIImage new]];