需求1:只有在首頁或者某一個頁面隱藏navigationController,其他頁面全部正常(系統(tǒng)導(dǎo)航欄,不包括自定義導(dǎo)航欄).
不怎么完美的解決辦法1:
1.設(shè)置透明圖片....
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
2.直接設(shè)置透明度...
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0;
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 1;
}
不怎么完美的解決辦法2:
此方法在返回時(手勢,返回按鈕)會有64的白條出現(xiàn)
- (void)viewWillAppear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:YES animated:NO];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:NO animated:NO];
[super viewWillDisappear:animated];
}
解決辦法3:
此方法是調(diào)用了系統(tǒng)動畫的方式在方法1的基礎(chǔ)上改進(jìn)
- (void)viewWillAppear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:NO animated:animated];
[super viewWillDisappear:animated];
}
解決辦法4:
導(dǎo)航欄代理方法,將需要影藏的控制器導(dǎo)航欄代理設(shè)置為self,然后調(diào)用代理方法,一個代理方法,一行代碼搞定
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[self.navigationController setNavigationBarHidden:[viewController isKindOfClass:[self class]] animated:YES];
}
需求2: navigationBar隨著scrollview偏移量改變透明度
不怎么完美的解決辦法1:
此方法即在界面出現(xiàn)的變化的時候刷新自己的導(dǎo)航欄
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if(self.tableView.y == 0){
self.navigationController.navigationBar.alpha = 1;
}
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self.navigationController.navigationBar.alpha = 1;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self refreshNavAlpha];
}
- (void)refreshNavAlpha{
float alpha = _lastY / 64;
alpha = alpha > 1 ? 1 : alpha;
if (alpha < 0) {
alpha = 0;
}
self.navigationController.navigationBar.alpha = alpha;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
_lastY = scrollView.contentOffset.y;
[self refreshNavAlpha];
}
相對完美的解決辦法:
尋找了很多APP,發(fā)現(xiàn)手機(jī)QQ6.7.1版本的導(dǎo)航欄,是采取漸變的方式,個人認(rèn)為,導(dǎo)航欄漸變還是需要自己處理好viewWillAppear方法中進(jìn)入時刷新navigationBar,可以根據(jù)方法1中的方法進(jìn)行改進(jìn),將navigationBar.alpha的透明度變化,增加一個漸變動畫即可,很簡單,即可和手機(jī)QQ的效果一樣!
- (void)refreshNavAlpha{
float alpha = _lastY / 64;
alpha = alpha > 1 ? 1 : alpha;
if (alpha < 0) {
alpha = 0;
}
[UIView animateWithDuration:0.2 animations:^{
self.navigationController.navigationBar.alpha = alpha;
}];
}
此Demo以及在后面補(bǔ)上