一、设置导航栏底线
- 简单获取底线
- (UIView *)navLine {
if (!_navLine) {
UIView *backgroundView = [self.navigationController.navigationBar subviews].firstObject;
_navLine = backgroundView.subviews.firstObject;
}
return _navLine;
}
2.单个页面设置隐藏显示
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navLine.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.navLine.hidden = NO;
}
二、设置导航栏背景图
1.设置导航栏背景图所需的各个尺寸
1倍图 640 * 128 px (一般用不到)
2倍图 750 * 128 px (5s,6,6s, 7)
3倍图 1242*192 px (6p, 6sp)
备注:其实一般两种图就足够,5s也是用2倍图,现在一般最低适配到5s,当然如果需求不一样就三张。
设置导航背景图代码
/*设置图片拉伸范围 如果图片被挤压变形的情况下*/
UIImage *backImage = [UIImage imageNamed:@"homeNav"];
backImage = [backImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 100, 10, 10)];
/*设置背景图*/
[self.navigationController.navigationBar setBackgroundImage:backImage forBarMetrics:UIBarMetricsDefault];
设置导航条背景图片时有时self.view会向下偏移64像素
//此句代码解决坐标问题
[self.navigationController.navigationBar setTranslucent:YES];
//当translucent = YES,controller中self.view的原点是从导航栏左上角开始计算
//当translucent = NO,controller中self.view的原点是从导航栏左下角开始计算
设置导航栏背景纯色
UINavigationBar *bar = [UINavigationBar appearance];
bar.barTintColor = [UIColor colorWithRed:62/255.0 green:173/255.0 blue:176/255.0 alpha:1.0];
设置导航栏title字体类型和大小
[self.navigationController.navigationBar setTitleTextAttributes:@{
NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:22],
NSForegroundColorAttributeName:[UIColor whiteColor]
}];
设置导航栏透明
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];``
[self.navigationController.navigationBar setShadowImage:nil];
}