导航栏透明度渐变处理逻辑(非自定义导航栏)
2018-10-17 本文已影响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;
}
- 控制变化
- (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;
}
}
至此完毕