前言:开发中经常碰到只有在首页或者某一个页面隐藏navigationController,其他页面全部正常.
1.如果是这样书写,会出现点击tabbar切换的时候,在回到需要隐藏navigationController的页面时会出现nav跳动问题
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
正确解决方式如下:
- (void)viewWillAppear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:YES animated:NO];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:NO animated:NO];
[super viewWillDisappear:animated];
}
这样也并不完美:来看第二种异常
2.假如是在首页隐藏nav,那么我们push到B页在返回时在nav位置则会出现明显的白色条状,如果是使用手势pop则更为明显;
综上正确解决方案如下:
- (void)viewWillAppear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:NO animated:animated];
[super viewWillDisappear:animated];
}
解释为什么要使用animated,我们点进API可以看到官方的注释
Hide or show the navigation bar. If animated, it will transition vertically using UINavigationControllerHideShowBarDuration.
大致意思是说隐藏和显示导航栏,如果是使用动画效果,他将使用UINavigationControllerHideShowBarDuration垂直过渡.