程序员社区

隐藏navgationbar底部的线,并且将底部设置为阴影

1、创建一个UIImageView,因为navgationbar底部的线其实就是一个imageView

@property (nonatomic, strong) UIImageView * navBarHairlineImageView;

2、找到这条线,隐藏或删除,

- (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;
}

3、在viewWillAppear中设置这些东西

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 设置底部阴影
    self.navigationController.navigationBar.layer.shadowColor = [UIColor blackColor].CGColor;
    self.navigationController.navigationBar.layer.shadowOffset = CGSizeMake(2, 2);
    self.navigationController.navigationBar.layer.shadowOpacity = 0.1;
    self.navigationController.navigationBar.layer.shadowRadius = 4.0;
    
    //隐藏底部细线
    _navBarHairlineImageView= [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    _navBarHairlineImageView.hidden = YES;
}

4、如果你在其他页面不需要此设置,那么你可以在viewWillDisappear中取消隐藏,或其他设置

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    _navBarHairlineImageView.hidden = NO;
}

还有一个问题就是,有可能除了底部的imageView还有其他的navgationbar的子视图,那么我们同理,可以循环着找到这些高度小于1,安全起见,我选择高度小于2的子视图都移除

@property (nonatomic, strong) UIView * navView;
@property (nonatomic, strong) UIView * navView1;
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.layer.shadowColor = [UIColor blackColor].CGColor;
    self.navigationController.navigationBar.layer.shadowOffset = CGSizeMake(2, 2);
    self.navigationController.navigationBar.layer.shadowOpacity = 0.1;
    self.navigationController.navigationBar.layer.shadowRadius = 4.0;

    _navBarHairlineImageView= [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    _navView = [self findHairlineViewUnder:self.navigationController.navigationBar];
    [_navView removeFromSuperview];
    _navView1 = [self findHairlineViewUnder:self.navigationController.navigationBar];
    [_navView1 removeFromSuperview];
    
    _navBarHairlineImageView.hidden = YES;
    _navView.hidden = YES;
    _navView1.hidden = YES;
}
// 找到所有高度小于2的线
- (UIView*)findHairlineViewUnder:(UIView*)view {
    for(UIView * subview in view.subviews) {
        if (subview.bounds.size.height < 2.0) {
            return subview;
        }
    }
    return nil;
}

以上方法应该可以解决了,如果还有的话,就多弄几个,直到不可能出现为止。

赞(0) 打赏
未经允许不得转载:IDEA激活码 » 隐藏navgationbar底部的线,并且将底部设置为阴影

一个分享Java & Python知识的社区