iOS隐藏导航条1px的底部横线

默认情况下会有这条线

iOS隐藏导航条1px的底部横线_第1张图片

第一种方法:

UINavigationBar *navigationBar = self.navigationController.navigationBar;
// white.png图片自己下载个纯白色的色块,或者自己ps做一个
[navigationBar setBackgroundImage:[UIImage imageNamed:@"white.png"]
                   forBarPosition:UIBarPositionAny
                       barMetrics:UIBarMetricsDefault];
[navigationBar setShadowImage:[UIImage new]];

运行后效果如下(此处原有的灰色背景色会被white.png代替):

iOS隐藏导航条1px的底部横线_第2张图片

PS:这是唯一一个隐藏这条线的官方用法,但是有一个缺陷-删除了translucency(半透明)

第二种方法:1)声明UIImageView变量,存储底部横线

@implementation MyViewController {
    UIImageView *navBarHairlineImageView;
}

2)在viewDidLoad中加入:

navBarHairlineImageView = [self findHairlineImageViewUnder:navigationBar];

3)实现找出底部横线的函数

- (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;
}

4)最后在viewWillAppear,viewWillDisappear中处理

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  navBarHairlineImageView.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  navBarHairlineImageView.hidden = NO;
}

效果如下:

iOS隐藏导航条1px的底部横线_第3张图片

PS:第二种方法可以保持bar的translucent

你可能感兴趣的:(【Objective-C】,【IOS移动端】)