iOS 去掉导航栏的边界黑线

2018-10-22  本文已影响11人  wvqusrtg

iOS 去掉导航栏的边界黑线

方法一:

 UIImage *img = [UIImage imageNamed:@""];
 [self.navigationController.navigationBar setBackgroundImage:img  forBarMetrics:UIBarMetricsDefault];
 self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];

注意:
1)如果img = [UIImage alloc]init] ; 导航条属性:translucency(半透明)。
2)建议使用不透明的背景图。一般高度:设置成64 = 导航栏的高度 + 状态栏的高度。

方法二:

1)、声明即将存储导航条下面那条横线的图片视图容器:

@interface XXXController () {
    UIImageView *navBarHairlineImageView;
}

2)、找到(viewDidLoad)那条黑色的横线:

INavigationBar *navigationBar = self.navigationController.navigationBar;
navBarHairlineImageView = [self  slnFindHairlineImageViewUnder:navigationBar];

解释:slnFindHairlineImageViewUnder 这是找到横线的方法。

(UIImageView *)slnFindHairlineImageViewUnder:(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 slnFindHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

3.需要在这两个viewWillAppear和viewWillDisappear方法里,进行处理一下:

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

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    navBarHairlineImageView.hidden = NO;
}
上一篇下一篇

猜你喜欢

热点阅读