1、隐藏导航栏
1)、
self.navigationController.navigationBar.alpha = 0;
等同于
nav.navigationBar.hidden = YES;
这种方法的原理是 navBar的本质是一个view 可以直接设置隐藏和透明度,但是他的位置没变 只是看不到了而已
而且用这行代码吧导航条隐藏掉
手势返回是依然可用的
2)、
nav.navigationBarHiidden = YES;
或者
[self.navigationController setNavigationBarHidden:!self.navigationController.navigationBarHidden animated:YES];
这个是系统支持的nav的方法,但是这个方法的弊端是
手势不可返回
如果要继续支持手势的话,需要手动添加一下方法,并添加代理
self.navigationController.interactivePopGestureRecognizer.delegate = self;
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
2、导航栏中部分viewController隐藏导航栏
效果可参照支付宝首页点击之后,
在需要隐藏的导航栏的viewController中添加
-(void)viewWillAppear:(BOOL)animated
{
[super <span style="font-family: Arial, Helvetica, sans-serif;">viewWillAppear:animated</span>];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
为什么要在will的方法添加而不是在did方法里面去添加?
原因在于:如果隐藏的viewController是nav的最后一个,ok,是没有问题的,但是如果隐藏了nav的VieController后面还有ViewController,那么就出现了问题,具体的可以自己去测试
更多请参考:
iOS导航栏的正确隐藏方式
3.获取已存在的导航条
在项目中 很多时候 会自定义导航条
[[[UIApplication sharedApplication].windows firstObject] rootViewController]
UIViewController *controller = (UIViewController *)[[[UIApplication sharedApplication] keyWindow] rootViewController];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:view];
nav.navigationBar.hidden = YES;
在导航条可手势返回的前提下
控制某一个页面不可手势返回
待验证
当从控制器A push到控制器B,我们返回控制器A,除了使用按钮返回
还可以使用ios7出来的向右滑动,返回控制器A
文档中是这样定义的:
@property(nullable, nonatomic, weak) id<UINavigationControllerDelegate> delegate;
@property(nullable, nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
我们在控制器B中的viewDidLoad中
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
self.navigationController.interactivePopGestureRecognizer.enabled = YES; // 手势有效设置为YES 无效为NO
self.navigationController.interactivePopGestureRecognizer.delegate = self; // 手势的代理设置为self
}
但是当回到控制器A中时,再想push到控制器B,就会出现卡屏,不会动的现象,因为rootView也会有向右滑动返回的问题, 要解决这个问题,我们只需在控制器A的viewDidAppear中设置,interactivePopGestureRecognizer为NO:
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
}
这样即可以保证再B中向右滑返回A动后再次pushB时不会卡在A界面。