美文网首页iOS学习开发
导航栏透明度渐变处理逻辑(非自定义导航栏)

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

作者: 姚姚先生 | 来源:发表于2018-10-17 16:55 被阅读3次

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

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

    至此完毕

    相关文章

      网友评论

        本文标题:导航栏透明度渐变处理逻辑(非自定义导航栏)

        本文链接:https://www.haomeiwen.com/subject/evurzftx.html