根据UI的设计,navigationbar需要跟界面一体化,但是下面这根黑线是比较烦的问题,可能界面一需要隐藏,界面二就要出现,也可能需要改变粗细之类的,又因为navigationbar会影响接下来的推栈,所以需要做一点小改动.
方法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:找出黑线,再做处理:
//通过一个方法来找到这个黑线(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;
}
//再定义一个imageview来等同于这个黑线
UIImageView *navBarHairlineImageView;
navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
同样的在界面出现时候开启隐藏
-(void)viewWillAppear:(BOOL)animated
{
navBarHairlineImageView.hidden = YES;
}
//在页面消失的时候就让出现
-(void)viewWillAppear:(BOOL)animated
{
navBarHairlineImageView.hidden = NO;
}
如果想要做一些更好的处理,比如说改变粗细,颜色之类的也在界面出现的时候写就行了.
推荐使用第二种方法,因为整个项目都在使用导航栏推栈,出栈,很可能因为改变了样式,导致后面的属性混乱起来.
网友评论
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
LoginVC *rootVC = [[LoginVC alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootVC];
[nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigation"]
forBarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
[nav.navigationBar setShadowImage:[UIImage new]];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
```
navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
navBarHairlineImageView.hidden = YES;
UIImageView *navLine = [[UIImageView alloc]initWithFrame:navBarHairlineImageView.frame];
navLine.backgroundColor = [UIColor blueColor];
[self.navigationController.view addSubview:navLine];
```