之前项目要求隐藏导航栏下的黑线,写了很多代码
后来在iOS10上不好使了
####1、之前用的这个方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated];
[self.navigationController.navigationBar.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if ([obj isKindOfClass:NSClassFromString(@"_UINavigationBarBackground")]){ UIView * v = obj; if (v.subviews.count) { navBottomLine = [v.subviews firstObject]; } * stop=YES; } }]; navBottomLine.hidden = YES; }
|
####2、iOS10出来之后,@"_UINavigationBarBackground"
变成了@"_UIBarBackground"
,代码改成这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #define iOS10 ([[UIDevice currentDevice].systemVersion intValue]>=10?YES:NO) - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self.navigationController.navigationBar.subviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) { if (iOS10) { if ([view isKindOfClass:NSClassFromString(@"_UIBarBackground")]) { [view.subviews firstObject].hidden = YES; } }else{ if ([view isKindOfClass:NSClassFromString(@"_UINavigationBarBackground")]) { [view.subviews firstObject].hidden = YES; } } }]; }
|
####3、可不可以精简一下呢:
如果设置了
[UINavigationBar appearance].translucent = NO;
1 2 3 4 5
| - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; self.navigationController.navigationBar.subviews[0].subviews[0].hidden = YES; }
|
否则
1 2 3 4 5
| - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; self.navigationController.navigationBar.subviews[0].subviews[1].hidden = YES; }
|
隐藏黑线之前和之后对比图
共
同
进
步
给个喜欢喔O(∩_∩)O