方法1:直接隐藏:
// 在页面出现的时候就将黑线隐藏起来 - (void)viewWillAppear:(BOOL)animated { [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; [self.navigationController.navigationBar setShadowImage:[UIImage new]]; } // 在页面消失的时候就让navigationbar还原样式 - (void)viewWillDisappear:(BOOL)animated{ [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault]; [self.navigationController.navigationBar setShadowImage:nil]; }
这个方法唯一的不好就是会影响导航栏的translucent(透明)属性
方法2:找出黑线,再做处理:
// 找到这个黑线 - (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]; _navBarHairlineImageView.hidden = YES; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; _navBarHairlineImageView.hidden = NO; }