第一种做法用动画的方式隐蔽导航栏,这样在利用滑动返回手势的时候效果最好,这样做有一个缺点就是在切换tabBar的时候有一个导航栏向上消失的动画.
- (void)viewWillAppearBOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappearBOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
第二种做法设置self为导航控制器的代理,实现代理方法,在将要表现控制器中设置导航栏隐蔽和表现,利用这种方式不仅完美切合滑动返回手势,同时也解决了切换tabBar的时候,导航栏动态隐蔽的问题。最后要记得在控制器销毁的时候把导航栏的代理设置为nil。
@interface HomeViewController () <UINavigationControllerDelegate>
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 设置导航控制器的代理为self
self.navigationController.delegate = self;
}
#pragma mark - UINavigationControllerDelegate
// 将要表现控制器
- (void)navigationControllerUINavigationController *)navigationController willShowViewControllerUIViewController *)viewController animatedBOOL)animated {
// 判断要表现的控制器是否是要隐蔽的控制器
BOOL isHome = [viewController isKindOfClass:[self class]];
[self.navigationController setNavigationBarHidden:isHome animated:YES];
}
- (void)dealloc {
self.navigationController.delegate = nil;
}
第三种做法
主要是针对A隐蔽Nav, A push 到B,B也必要隐蔽Nav的这种情况 主要是在自定义UINavigationController文件内举行利用的
自定义UINavigationController
#import "NavigationController.h"
#import "HomeViewController.h"
#import "HomeViewController_two.h"
@interface NavigationController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>
@end
@implementation NavigationController
- (void)viewDidLoad {
[super viewDidLoad];
self.delegate = self;
// 设置全屏滑动返回
id target = self.interactivePopGestureRecognizer.delegate;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target actionselector(handleNavigationTransition];
[self.view addGestureRecognizer:pan];
self.interactivePopGestureRecognizer.enabled = NO;
}
- (void)pushViewControllerUIViewController *)viewController animatedBOOL)animated{
if (self.viewControllers.count > 0) {
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
}
#pragma mark - UINavigationControllerDelegate
- (void)navigationControllerUINavigationController *)navigationController willShowViewControllerUIViewController *)viewController animatedBOOL)animated {
// 判断如果是必要隐蔽导航控制器的类,则隐蔽
BOOL isHideNav = ([viewController isKindOfClass:[HomeViewController class]] ||
[viewController isKindOfClass:[HomeViewController_two class]]);
//setNavigationBarHidden:YES设置这行代码后会导致Nav的滑动返回手势失效,这也就是为什么前面我们在自定义导航的时候必要设置全屏滑动返回了
[self setNavigationBarHidden:isHideNav animated:YES];
}
————————————————
版权声明:本文为博主原创文章,遵照 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_33646395/article/details/84951429
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |