在项目中,有一个界面的导航栏颜色需要与视图的背景色相同,在调试的时候发现,设置的导航栏颜色与视图背景色一样时,实际看到的缺不一样,而且导航栏与视图之间有一条很细的线。于是在网上找了一些方法来解决这个问题。如下:
#import "ViewController.h"
@interface ViewController ()
{
UIImageView *navBarBottonLineImageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
navBarBottonLineImageView = [self findBarBottomLineImageView:self.navigationController.navigationBar];
// test
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.view.backgroundColor = [UIColor redColor];
self.navigationController.navigationBar.translucent = NO; //设置半透明度为NO后,导航栏的颜色才与视图背景色一样
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
navBarBottonLineImageView.hidden = YES;//隐藏
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
navBarBottonLineImageView.hidden = NO;
}
// 找到横线视图
- (UIImageView *)findBarBottomLineImageView:(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 findBarBottomLineImageView:subView];// 递归
if (imageView) {
return imageView;
}
}
return nil;
}
@end
1.没有设置self.navigationController.navigationBar.translucent = NO;
和navBarBottonLineImageView.hidden = YES
时,原始效果:
2.设置了self.navigationController.navigationBar.translucent = NO
时效果图:
3.设置navBarBottonLineImageView.hidden = YES
后的效果图: