我们只需要将需要进行导航栏隐藏与展示的vc放到baseNavigationController控制器下,并在该控制器的代理里去操作指定控制器的导航栏的隐藏即可,无需在控制器里做任何操作。
具体实现如下:
#import "BaseNavigationController.h"
#import "NoNavTargetVC.h"
@interface BaseNavigationController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>
@end
@implementation BaseNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
self.delegate = self;
id target = self.interactivePopGestureRecognizer.delegate;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:NSSelectorFromString(@"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 {
BOOL isHideNav = [viewController isKindOfClass:[NoNavTargetVC class]];
NSLog(@"isHide = %d", isHideNav);
[self setNavigationBarHidden:isHideNav animated:YES];
}
@end
也可以这样:
在需要隐藏的VC里重写:
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self.navigationController.navigationBarHidden = NO;
}
网友评论