ios开发中经常碰到各种需求,比如要求导航栏的颜色和self.view的颜色一样,当我们直接设置navigationBar的颜色和view一样时,我们会发现navigationBar还会有一条分割线留下,本文就是介绍怎么隐藏导航栏底部的分割线的。
第一种方法:不会影响导航栏translucent 半透明的显示
首先在viewDidLoad 定义一个 UIImageView *navigationImageView 来存储底部分割线。
UIImageView *navigationImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
self.navigationImageView = navigationImageView;
-(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;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated]; self.navigationImageView.hidden = YES;
}
-(void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated]; self.navigationImageView.hidden = NO;
}
- 第二种方法:会删除导航栏 translucent 半透明的显示。
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navImage"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];