11 ios 导航栏黑线

2016-08-25 09:49:08 u010407865 阅读数 2172

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    

    ViewController *viewCon = [[ViewController alloc] init];

    

    UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:viewCon];

    //设置导航栏背景颜色

    navCon.navigationBar.barTintColor = [UIColor colorWithRed:13.0/255 green:125.0/255 blue:220.0/255 alpha:1];

    //设置导航栏不透明,透明时,导航栏在屏幕最上层,即设置Subviewframe是加64,不透明时,导航栏与ViewController在同一层,即设置Subviewframe是不加64

    [navCon.navigationBar setTranslucent:NO];

    UIImageView *navBarHairlineImageView = [self findHairlineImageViewUnder:navCon.navigationBar];

    navBarHairlineImageView.hidden = YES;

    

    self.window.rootViewController = navCon;

    

    return YES;

}


//通过一个方法来找到这个黑线(findHairlineImageViewUnder):

- (UIImageView *)findHairlineImageViewUnder:(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 findHairlineImageViewUnder:subview];

        if (imageView) {

            return imageView;

        }

    }

    return nil;

}

2016-04-28 17:24:01 klshuo 阅读数 554
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
    [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
2016-05-21 15:17:03 hero_wqb 阅读数 15003

去掉导航栏self.navigationController.navigationBar下默认黑线。

方法一:(会影响导航栏的translucent透明属性

//视图将要显示时隐藏
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

//视图将要消失时取消隐藏
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}
方法二:

@property (nonatomic, weak) UIImageView *lineView;

//视图加载完成获取到导航栏最下面的黑线
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //获取导航栏下面黑线
    _lineView = [self getLineViewInNavigationBar:self.navigationController.navigationBar];
}

//视图将要显示时隐藏
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    _lineView.hidden = YES;
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
}

//视图将要消失时取消隐藏
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    _lineView.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
}

//找到导航栏最下面黑线视图
- (UIImageView *)getLineViewInNavigationBar:(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 getLineViewInNavigationBar:subview];
        if (imageView) {
            return imageView;
        }
    }
    
    return nil;
}



2017-10-30 09:45:11 YJLiOS 阅读数 1360

在项目有时候需要去除掉导航栏下面的黑线,现在附上代码,供大家参考

@property (nonatomic, weak) UIImageView * lineView;


//1.视图加载完成获取到导航栏最下面的黑线
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //2.获取导航栏下面的黑线
    self.lineView = [self getLineViewInNavigationBar:self.navigationController.navigationBar];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //3.视图将要显示时隐藏
    self.lineView.hidden = YES;
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //4.视图将要消失时取消隐藏
    self.lineView.hidden = NO;
}

//找到导航栏最下面黑线视图
- (UIImageView *)getLineViewInNavigationBar:(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 getLineViewInNavigationBar:subview];
        if (imageView) {
            return imageView;
        }
    }
    
    return nil;
}


2017-06-01 10:02:17 chenzheiu1210 阅读数 892

项目需求 导航栏要求显示红色,改了之后 看到底部有条细细的黑线,巨丑~

隐藏黑线:



隐藏黑线

然后发现 切换界面其他界面的黑线没了,巨丑~

要在当前界面切出的时候添加显示黑线的代码

显示黑线:


显示黑线

至此,解决问题

这篇总结很详细:http://www.jianshu.com/p/effa4a48f1e3