情景: 一级页面不显示导航栏 ,二级页面显示导航栏.
方法一 适用于push/pop:
一级页面中
- (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]; }
方法二(都适用):
设置self为导航控制器的代理,实现代理方法,在将要显示控制器中设置导航栏隐藏和显示,使用这种方式不仅完美切合滑动返回手势,同时也解决了切换tabBar的时候,导航栏动态隐藏的问题.
@interface HomePageController () <UINavigationControllerDelegate> @end @implementation HomePageController #pragma mark - lifeCycle - (void)viewDidLoad { [super viewDidLoad]; // 设置导航控制器的代理为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]; }
写个基类viewcontroller,只要在基类viewcontroller中:
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:self.navigationHidden animated:animated]; }
这样你就随心所欲的控制了,只要你想要hidden。给navigationHidden属性赋值yes就好了。