去掉导航栏下边的横条

去掉导航栏下边的横条

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

#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后的效果图:

    这里写图片描述

猜你喜欢

转载自blog.csdn.net/sinat_34194127/article/details/51897965