隐藏/改变导航条底部的黑线

直接上方法:

1.隐藏黑线:(会影响navigationBar的translucent属性)

- (void)viewWillDisappear:(BOOL)animated{

    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}

 
如果不想影响下个界面的效果: 

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

2.遍历找出这条黑线,再做相应的处理:

先写个方法,用来找出这条黑线:

 - (UIImageView *)findBottomLineInView:(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 findBottomLineInView:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}
然后再做相应处理:

- (void)viewWillAppear:(BOOL)animated {
    UIImageView *navBarBottomLine = [self findBottomLineInView:self.navigationController.navigationBar];
    navBarBottomLine.hidden = YES;
}
 

 
也可以自定义view直接覆盖: 
 UIImageView *navLine = [[UIImageView alloc] initWithFrame:navBarBottomLine.frame];
 navLine.backgroundColor = [UIColor RedColor];
 [self.navigationController.view addSubview:navLine];
 navLine.backgroundColor = [UIColor blueColor];
 [self.navigationController.view addSubview:navLine];

 
3.利用setShadowImage:方法 

给UIImage写个分类---->用颜色值来创建一张图片

+ (UIImage *)imageWithColor:(UIColor *)color withSize:(CGSize)size
{
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1.0] withSize:CGSizeMake(self.view.frame.size.width, 1)] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:[UIColor colorWithRed:199/255.0 green:199/255.0 blue:199/255.0 alpha:0.5] withSize:CGSizeMake(self.view.frame.size.width, 1)]];
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:50/255.0 green:50/255.0 blue:50/255.0 alpha:1.0]];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:136/255.0 green:240/255.0 blue:0 alpha:1.0] withSize:CGSizeMake(self.view.frame.size.width, 1)] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
}

这样就可以做到分开单独设置或者统一设置



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值