
NagigationBar.gif
簡介
在項目中經(jīng)常碰到首頁頂部是無限輪播,需要靠最上面顯示.有的設(shè)置導(dǎo)航欄為透明等一系列的方法,這個可以借助第三方.或者干脆簡單粗暴的直接隱藏掉導(dǎo)航欄.可是push到下一個頁面的時候是需要導(dǎo)航欄的,如何做了,這里給出兩種方法.
第一種做法
注意這里一定要用動畫的方式隱藏導(dǎo)航欄,這樣在使用滑動返回手勢的時候效果最好,和上面動圖一致.這樣做有一個缺點就是在切換tabBar的時候有一個導(dǎo)航欄向上消失的動畫.
- (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)航控制器的代理,實現(xiàn)代理方法,在將要顯示控制器中設(shè)置導(dǎo)航欄隱藏和顯示,使用這種方式不僅完美切合滑動返回手勢,同時也解決了切換tabBar的時候,導(dǎo)航欄動態(tài)隱藏的問題。最后要記得在控制器銷毀的時候把導(dǎo)航欄的代理設(shè)置為nil。
@interface WLHomePageController () <UINavigationControllerDelegate>
@end
@implementation WLHomePageController
#pragma mark - lifeCycle
- (void)viewDidLoad {
[super viewDidLoad];
// 設(shè)置導(dǎo)航控制器的代理為self
self.navigationController.delegate = self;
}
#pragma mark - UINavigationControllerDelegate
// 將要顯示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 判斷要顯示的控制器是否是自己
BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
[self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
- (void)dealloc {
self.navigationController.delegate = nil;
}
第三種做法
主要是針對A隱藏Nav, A push 到B,B也需要隱藏Nav的這種情況
1、自定義UINavigationController
#import "WYNavigationController.h"
#import "ViewController.h"
#import "WYTargetVC.h"
@interface WYNavigationController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>
@end
@implementation WYNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
self.delegate = self;
// 設(shè)置全屏滑動返回
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:[ViewController class]] ||
[viewController isKindOfClass:[WYTargetVC class]]);
[self setNavigationBarHidden:isHideNav animated:YES];
}
但是注意setNavigationBarHidden:YES設(shè)置這行代碼后會導(dǎo)致Nav的滑動返回手勢失效,這也就是為什么前面我們在自定義導(dǎo)航的時候需要設(shè)置全屏滑動返回了。在這里可能有人會說我不想要全屏返回,我就想要原本系統(tǒng)滑動返回的樣式,怎么辦。歡迎大家在留言區(qū)發(fā)表你的看法。