1 //去除bar下方 线 2 if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){ 3 NSArray *list=self.navigationController.navigationBar.subviews; 4 for (id obj in list) { 5 if ([obj isKindOfClass:[UIImageView class]]) { 6 UIImageView *imageView=(UIImageView *)obj; 7 NSArray *list2=imageView.subviews; 8 for (id obj2 in list2) { 9 if ([obj2 isKindOfClass:[UIImageView class]]) { 10 UIImageView *imageView2=(UIImageView *)obj2; 11 imageView2.hidden=YES; 12 } 13 } 14 } 15 } 16 }
2、iOS10出来之后,@"_UINavigationBarBackground" 变成了@"_UIBarBackground",代码改成这样: #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) { //iOS10,改变了导航栏的私有接口为_UIBarBackground if ([view isKindOfClass:NSClassFromString(@"_UIBarBackground")]) { [view.subviews firstObject].hidden = YES; } }else{ //iOS10之前使用的是_UINavigationBarBackground if ([view isKindOfClass:NSClassFromString(@"_UINavigationBarBackground")]) { [view.subviews firstObject].hidden = YES; } } }]; } 可不可以精简一下呢: 如果设置了 [UINavigationBar appearance].translucent = NO; - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; self.navigationController.navigationBar.subviews[0].subviews[0].hidden = YES; } 否则 - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; self.navigationController.navigationBar.subviews[0].subviews[1].hidden = YES; }