在項(xiàng)目中經(jīng)常碰到首頁頂部是無限輪播,需要靠最上面顯示.有的設(shè)置導(dǎo)航欄為透明等一系列的方法或者干脆簡(jiǎn)單粗暴的直接隱藏掉導(dǎo)航欄. 可一般情況下當(dāng)push到下一個(gè)頁面的時(shí)候又需要展示導(dǎo)航欄的。這里給出了三種方法.
第一種做法
注意這里一定要用動(dòng)畫的方式隱藏導(dǎo)航欄,如果不這樣使用滑動(dòng)返回手勢(shì)的時(shí)候顯的非常僵硬欢顷。為了使用滑動(dòng)返回手勢(shì)的時(shí)候效果最好,所以我推薦用動(dòng)畫的方式.這樣做有一個(gè)缺點(diǎn)就是在切換tabBar的時(shí)候有一個(gè)導(dǎo)航欄向上消失的動(dòng)畫.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
第二種做法
設(shè)置self為導(dǎo)航控制器的代理,實(shí)現(xiàn)代理方法,在將要顯示控制器中設(shè)置導(dǎo)航欄隱藏和顯示,使用這種方式不僅完美切合滑動(dòng)返回手勢(shì),同時(shí)也解決了切換tabBar的時(shí)候,導(dǎo)航欄動(dòng)態(tài)隱藏的問題喉脖。
tip:記得在控制器銷毀的時(shí)候把導(dǎo)航欄的代理設(shè)置為nil。
@interface CSViewController () <UINavigationControllerDelegate>
@end
@implementation CSViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 設(shè)置導(dǎo)航控制器的代理為self
self.navigationController.delegate = self;
}
#pragma mark - UINavigation-Delegate
// 將要顯示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 判斷要顯示的控制器是否是自己
BOOL isShowVC = [viewController isKindOfClass:[self class]];
[self.navigationController setNavigationBarHidden:isShowVC animated:YES];
}
- (void)dealloc {
self.navigationController.delegate = nil;
}
第三種做法
主要是針對(duì)A隱藏Nav, A push 到B,B也需要隱藏Nav的這種情況
1、自定義UINavigationController
#import "CSNavigationController.h"
#import "ONEViewController.h"
#import "TWOViewController.h"
@interface CSNavigationController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>
@end
@implementation CSNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
self.delegate = self;
// 設(shè)置全屏滑動(dòng)返回
id target = self.interactivePopGestureRecognizer.delegate;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
[self.view addGestureRecognizer:pan];
self.interactivePopGestureRecognizer.enabled = NO;
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.viewControllers.count > 0) {
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
}
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 判斷如果是需要隱藏導(dǎo)航控制器的類,則隱藏
BOOL isHideNav = ([viewController isKindOfClass:[ONEViewController class]] ||
[viewController isKindOfClass:[TWOViewController class]]);
[self setNavigationBarHidden:isHideNav animated:YES];
}
但是注意setNavigationBarHidden:YES設(shè)置這行代碼后會(huì)導(dǎo)致Nav的滑動(dòng)返回手勢(shì)失效界拦,這也就是為什么前面我們?cè)谧远x導(dǎo)航的時(shí)候需要設(shè)置全屏滑動(dòng)返回了吸申。