1.设置背景色--backgroundColor
UINavigationBar *bar= self.navigationController.navigationBar;
bar.backgroundColor= [UIColor redColor];
效果:不透明,不是我们想要的纯红色;
查看原因及分析:整个导航栏看上去之所以呈现淡红色,是因为上面还有几层遮盖,且遮盖并非cleancolor,效果叠加,所以显示出来不为纯红色。还有一点值得注意,导航栏下面颜色深,而上面一部分颜色较浅,这是因为UINavigationBar的高度为44,而其上面的视图的高度为64。
2.设置背景图片--BackgroundImage
首先封装了一个方法,用来生成背景图片
- (UIImage*) imageWithFrame:(CGRect)frame alphe:(CGFloat)alphe{
frame =CGRectMake(0,0, frame.size.width, frame.size.height);
UIColor*redColor = [UIColorcolorWithRed:1green:0blue:0alpha:alphe];
UIGraphicsBeginImageContext(frame.size);
CGContextRefcontext =UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [redColorCGColor]);
CGContextFillRect(context, frame);
UIImage*theImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
设置背景图片(这里的α= 1.0)
UINavigationBar *bar= self.navigationController.navigationBar;
UIImage *bgImage= [self imageWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width,64) alphe:1.0];
[barsetBackgroundImage:bgImageforBarMetrics:UIBarMetricsDefault];
查看原因及分析:UINavigationBarBackground被设置为红色,与设置的backgroundColor相比少了两层查看:
在这里图片的阿尔法值需要注意:无论图片阿尔法值是否等于1,都会有半透明效果,值越大不透明度越高,但是不可能变成完全不透明(按理来说,我们设置的图片的α= 1.0,应该是不透明才对,但是这里仍然半透明,这一点在后面会解释)
3.设置barTintColor
UINavigationBar *bar= self.navigationController.navigationBar;
bar.barTintColor= [UIColor redColor];
参考:http://www.jianshu.com/p/d0dbba7f3abc