1.滑动返回如何失效
1.自定义leftBarButtonItem
2.导航栏隐藏的时候
[self.navigationController setNavigationBarHidden:hiden animated:animated];
3.有UIScrollView存在的时候
滑动返回如何再次生效
第一种和第二种
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
第三种
[_scrollView.panGestureRecognizer requireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];
如何禁止滑动返回
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// 禁用返回手势
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
}
如果是单一页面我们最好是在
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
}
最好使用上面的。还有一种方式
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
return NO;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
//- (void)viewDidDisappear:(BOOL)animated {
// [super viewDidDisappear:animated];
// self.navigationController.interactivePopGestureRecognizer.delegate = nil;
//}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
关键点是设置nil的时机在viewWillDisappear:的位置上
在viewDidDisappear:设置的话是没有效果的,必须在画面关闭之前设置好
全屏的滑动返回
- (void)viewDidLoad {
[super viewDidLoad];
// 获取系统自带滑动手势的target对象
id target = self.interactivePopGestureRecognizer.delegate;
// 创建全屏滑动手势,调用系统自带滑动手势的target的action方法
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
// 设置手势代理,拦截手势触发
pan.delegate = self;
// 给导航控制器的view添加全屏滑动手势
[self.view addGestureRecognizer:pan];
// 禁止使用系统自带的滑动手势
self.interactivePopGestureRecognizer.enabled = NO;
}
// 什么时候调用:每次触发手势之前都会询问下代理,是否触发。
// 作用:拦截手势触发
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
// 注意:只有非根控制器才有滑动返回功能,根控制器没有。
// 判断导航控制器是否只有一个子控制器,如果只有一个子控制器,肯定是根控制器
if (self.childViewControllers.count == 1) {
// 表示用户在根控制器界面,就不需要触发滑动手势,
return NO;
}
return YES;
}
页面消失会调用的方法. 应该是 pop和push
- (void)willMoveToParentViewController:(UIViewController*)parent {
[super willMoveToParentViewController:parent];
if (!parent) {
NSLog(@"will 离开父试图");
} else {
NSLog(@"will 加在父试图");
}
}
- (void)didMoveToParentViewController:(UIViewController*)parent {
[super didMoveToParentViewController:parent];
if (!parent) {
NSLog(@"did 离开父试图");
} else {
NSLog(@"did 加在父试图");
}
}
参考:【8行代码教你搞定导航控制器全屏滑动返回效果】
网友评论