导航栏透明度渐变处理逻辑(非自定义导航栏)

根据列表滚动距离控制导航栏的透明度(非隐藏导航栏自定义)

1.自定义View, 放在导航栏来控制颜色变化

- (UIImageView *)bgView{
    if (_bgView == nil) {
        _bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.navigationController.navigationBar.bounds.size.width, self.navigationController.navigationBar.bounds.size.height+20)];
        [self.navigationController.view insertSubview:_bgView belowSubview:self.navigationController.navigationBar];
    }
    return _bgView;
}
  1. 控制变化
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    self.bgView.image = [UIImage imageNamed:@"navBarImage"];
    self.bgView.alpha = alpha;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] rect:CGRectMake(0, 0, KScreenWidth, kNavigationBarHeight)] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:[UIColor colorFromString:@"#DCDCDC"] rect:CGRectMake(0, 0, KScreenWidth, .5)]];
    self.bgView.image = [UIImage yy_imageWithColor:[UIColor whiteColor]];
    [self.bgView removeFromSuperview];
    self.bgView = nil;
}

3.实现滑动代理方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    alpha = scrollView.contentOffset.y/120;
    if (alpha > 1) {
        alpha = 1;
    }
    if (scrollView == self.tableView) {
        self.bgView.image = [UIImage imageNamed:@"navBarImage"];
        self.bgView.alpha = alpha;
    }
}

至此完毕

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容