设置导航栏的背景颜色
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:53.0/255.0 green:217.0/255.0 blue:142.0/255.0 alpha:1];
/* 这里不能直接设置backgroundColor */
设置后发现跟设置的颜色值有偏差,查了下应该是上面navigationBar有一层透明视图的原因,添加如下代码
The default value is YES. If the navigation bar has a custom background image, the default is YES if any pixel of the image has an alpha value of less than 1.0, and NO otherwise.
If you set this property to YES on a navigation bar with an opaque custom background image, the navigation bar will apply a system opacity less than 1.0 to the image.
If you set this property to NO on a navigation bar with a translucent custom background image, the navigation bar provides an opaque background for the image using black if the navigation bar has UIBarStyleBlack style, white if the navigation bar has UIBarStyleDefault, or the navigation bar’s barTintColor if a custom value is defined.
默认为YES。导航栏有背景图片的情况下,如过这个图片中的任意一个像素透明度小于1,则默认为YES,否则为NO。
如果当导航栏有一个自定义背景图片的情况下,你设置translucent为YES,导航栏会给这张图片加上一个透明度小于1的视图。
导航栏在有一个半透明背景图片时候你设置translucent为NO,导航栏会给这个半透明图片加上一层不透明的试图,如果导航栏样式为UIBarStyleBlack则添加一层黑色试图,如果导航栏样式为UIBarStyleDefault或者导航栏已经设置过barTintColor属性则添加一层白色试图
self.navigationController.navigationBar.translucent = NO;
一个坑:原图上采色计采集的数值跟模拟器上显示出的图片采集的数值不一样不一样不一样啊 设置barTintColor的数值要在原图上去采集
原图:53/217/142
模拟器图:60/223/153
导航栏颜色:68/227/163 // translucent为YES RGB采集的模拟器上图片的数值
导航栏去横线
横线理解为UINavegationBar的shadowImage属性,文档中这样说明
For a custom shadow image to be shown, a custom background image must also be set with the setBackgroundImage:forBarMetrics: method. If the default background image is used, then the default shadow image will be used regardless of the value of this property.
大致意思是说要设置shadowImage必须先要通过
setBackgroundImage:forBarMetrics:方法给UINavgationBar设置一个背景图片
未设置前
//UIBarMetricsDefault-竖屏横屏都有,横屏导航条变宽,则自动repeat图片
//UIBarMetricsCompact-竖屏没有,横屏有
加上下面代码
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
效果
另一种方法:
@interface ViewController ()
{
UIImageView *navBarHairlineImageView; // 系统默认的那条横线图片
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationController.navigationBar.translucent = NO;
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:53.0/255.0 green:217.0/255.0 blue:142.0/255.0 alpha:1];
// [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
// [self.navigationController.navigationBar setShadowImage:[UIImage new]];
navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
}
/* 可以在这两个方法里控制是否显示横条
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
navBarHairlineImageView.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
navBarHairlineImageView.hidden = NO;
}
*/
// 找到那条横线
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
return (UIImageView *)view;
}
for (UIView *subview in view.subviews) {
UIImageView *imageView = [self findHairlineImageViewUnder:subview];
if (imageView) {
return imageView;
}
}
return nil;
}