iOS小知识点08(去掉导航栏下边的横条)

在项目中,有一个界面的导航栏颜色需要与视图的背景色相同,在调试的时候发现,设置的导航栏颜色与视图背景色一样时,实际看到的缺不一样,而且导航栏与视图之间有一条很细的线。于是在网上找了一些方法来解决这个问题。如下:

#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时,原始效果:

iOS小知识点08(去掉导航栏下边的横条)_第1张图片
原始图

2.设置了self.navigationController.navigationBar.translucent = NO时效果图:

iOS小知识点08(去掉导航栏下边的横条)_第2张图片
颜色一致

3.设置navBarBottonLineImageView.hidden = YES后的效果图:

iOS小知识点08(去掉导航栏下边的横条)_第3张图片
隐藏导航栏下边的横条

你可能感兴趣的:(iOS小知识点08(去掉导航栏下边的横条))