iOS中自定义导航栏底部横线

iOS自带的导航栏底部横线比较粗,有时候我们想自定义,可以更改粗细或者颜色之类的。
//通过一个方法来找到这个黑线(findHairlineImageViewUnder):

- (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:YES];
_navBarHairlineImageView.hidden = YES;
}

界面消失时候显示

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

在viewDidLoad中

- (void)viewDidLoad {
    [super viewDidLoad];
    _navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    _navBarHairlineImageView.hidden = YES;
    UIImageView *navLine = [[UIImageView alloc]initWithFrame:_navBarHairlineImageView.frame];
//    navLine.backgroundColor = [UIColor blueColor];
    navLine.image = [UIImage imageNamed:@"GrayLine"];//可以对navLine进行操作
    [self.navigationController.view addSubview:navLine];
}

在这里我们用自定义的UIimageview代替导航栏底部。

2019.3.20更新
影藏导航栏底部横线

self.navigationController.navigationBar.clipsToBounds = YES;

你可能感兴趣的:(iOS中自定义导航栏底部横线)