參考 http://www.cocoachina.com/ios/20170331/18944.html
先看一下UINavigaionBar的層次結(jié)構(gòu)
Paste_Image.png
1. 獲取UIBarbackgroud視圖问慎,改變其透明度壕鹉。
self.barImageView = self.navigationController.navigationBar.subviews.firstObject;
2. 設(shè)置導航欄的背景圖
在合適的地方設(shè)置導航欄的背景圖产园,我是在AppDelegate中設(shè)置的
[nav.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault]; //設(shè)置背景
3.在UIScrollView的delegate方法改變改變導航欄的背景的透明度
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"y = %f",scrollView.contentOffset.y);
CGFloat offsetY = scrollView.contentOffset.y;
if(offsetY <= 85){
CGFloat alpha = MIN(1,(offsetY)/150 );
_barImageView.alpha = alpha;
}else{
_barImageView.alpha = 1;
}
NSLog(@"alpha = %f",_barImageView.alpha);
}
4.在viewWillAppear中設(shè)置_barImageView.alpha = 0
而且設(shè)置UIScrollView(tableView)的delegate = nil
viewWillDisapper同理
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.tableView.delegate = self;
_barImageView.alpha = 0 ;
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self.tableView.delegate = nil;//為了防止頁面push時霞玄,導航欄的背景還發(fā)生改變
_barImageView.alpha = 1 ;
}
https://github.com/JnLuffy/LFY_UINavigationController.navigationBar