話不多說直接上代碼
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// 隱藏導(dǎo)航欄
//? ? [self.navigationController.navigationBar setHidden:YES];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
-(void) viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
//顯示導(dǎo)航欄
//? ? [self.navigationController.navigationBar setHidden:NO];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//scrollView已經(jīng)有拖拽手勢滞欠,直接拿到scrollView的拖拽手勢
UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer;
//獲取到拖拽的速度 <0 向下拖動 >0 向上拖動
CGFloat velocity = [pan velocityInView:scrollView].y;
if (scrollView.contentOffset.y < 0) {
//向上拖動,隱藏導(dǎo)航欄
[self.navigationController setNavigationBarHidden:YES animated:YES];
}else if (scrollView.contentOffset.y > 0){
[self.navigationController setNavigationBarHidden:NO animated:YES];
//透明度
//找最大值/
//? ? ? ? ? ? CGFloat alpha = fabs(scrollView.contentOffset.y * 1 / 136.0);? // (200 - 64) / 136.0f
//計(jì)算透明度
CGFloat alpha = scrollView.contentOffset.y /64 >1.0f ? 1:scrollView.contentOffset.y/64;
if (alpha >= 1) {
// 透明度最大值
alpha = 0.99;
}
self.alphaImage = [UIImage imageWithColor:[GLOBAL_BLUE colorWithAlphaComponent:alpha]];
//? ? ? ? [self.navigationController setDefaultNavigationBar];
//修改導(dǎo)航條背景圖片
[self.navigationController.navigationBar setBackgroundImage:self.alphaImage forBarMetrics:UIBarMetricsDefault];
}else if(velocity == 0){
//停止拖拽
}
}
這些還不夠(透明度沒有)粘昨,由于該控制器繼承了父控制器,所以得在父控制器中判斷來開啟透明度
UIViewController *vc = self;
BOOL isTrue = [vc isMemberOfClass:[PositionDetailViewController class]] || [vc isMemberOfClass:[ActivityDetailViewController class]];
if (isTrue) {
//導(dǎo)航欄自帶的毛玻璃效果
self.navigationController.navigationBar.translucent = YES;
}else{
self.navigationController.navigationBar.translucent = NO;
}
問題又來了倾剿,pop或者push回去的控制器的導(dǎo)航欄會跟著變化,解決方案請看如下代碼:
在上級控制器中加上以下代碼:
//防止進(jìn)入子頁面再pop回來變成透明
-(void) viewWillAppear:(BOOL)animated{
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"daohanglan"] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.translucent = NO;
}