导航控制器的interactivePopGestureRecognizer还存在,但没有起作用,
可能是delegate里被阻断了没调用target/action,或者是调用了target/action没运行动画
自定义返回按钮或者隐藏navigationBar导致的该手势未起作用是因为在delegate阶段被阻断了
右滑返回解决
@implementation LXFNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
// 记录系统手势代理
self.popGesture = self.interactivePopGestureRecognizer;
self.delegate = self;
}
#pragma mark - UINavigationControllerDelegate
// 当控制器显示完毕的时候调用
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 根据 栈 先进后出
if (self.viewControllers[0] == viewController) { // 根控制器
// 还原代理
self.interactivePopGestureRecognizer.delegate = self.popGesture;
} else { // 非控制器
// 清空手势代理就能实现滑动返回,iOS6不支持
self.interactivePopGestureRecognizer.delegate = nil;
}
// 如果当前控制器为根控制器,则使手势失效,不然手势会将根控制器移除
if (self.viewControllers.count == 1) {
self.interactivePopGestureRecognizer.enabled = NO;
} else {
self.interactivePopGestureRecognizer.enabled = YES;
}
}
@end
最简化版
- (void)viewDidLoad {
[super viewDidLoad];
self.interactivePopGestureRecognizer.delegate = self;
self.delegate = self;
}
#pragma mark - UINavigationControllerDelegate
// 当控制器显示完毕的时候调用
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
self.interactivePopGestureRecognizer.enabled = YES;
// 解决某些情况下push时的假死bug,防止把根控制器pop掉
if (navigationController.viewControllers.count == 1) {
self.interactivePopGestureRecognizer.enabled = NO;
}
}
当控制器为根控制器时,手势还在起作用,相当于对根控制器做了pop操作,
这会出现一个错误nested pop animation can result in corrupted navigation bar
看起来没任何变化,但是再次push其它控制器时候,就会出现这个问题了
解决某些情况下push时的假死bug
导致这个错误的原因还有一个,如果我们pop的动画正在执行,再去触发一次手势,会导致导航控制器和导航条的动画混乱。
为了避免问题出现我们需要成为手势的代理,判断当前控制器是否为根控制器并且pop或者push动画是否在执行(这个变量是私有的,需要用kvc来获取)
- navigationController的手势,一个vc中禁用,所有右滑返回都不能用
- navgationBar隐藏时,右滑返回效果很丑
/**
// 什么都没有 -> 不能滑动返回
🎈 self.navigationController.interactivePopGestureRecognizer = <UIScreenEdgePanGestureRecognizer: 0x7f9cedf15240; state = Possible; delaysTouchesBegan = YES; view = <UILayoutContainerView 0x7f9cede14880>; target= <(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition 0x7f9cedf15100>)>>
🎈 self.navigationController.interactivePopGestureRecognizer.delegate = <_UINavigationInteractiveTransition: 0x7f9cedf15100>
// delegate == nil -> 能滑动返回
// 重大bug: 返回到根控制器,再右滑 点push按钮进不去了
// 到根控制器,如果不右滑动👉 push按钮能点进去
🎈 self.navigationController.interactivePopGestureRecognizer = <UIScreenEdgePanGestureRecognizer: 0x7faae8508fe0; state = Possible; delaysTouchesBegan = YES; view = <UILayoutContainerView 0x7faae8612640>; target= <(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition 0x7faae8508ea0>)>>
📍-[DemoViewController viewDidAppear:] + 65🎈 self.navigationController.interactivePopGestureRecognizer.delegate = (null)
// delegate == self -> 和nil同样的问题!
// 重大bug: 返回根控制器,再右滑 再点push按钮进不去了
🎈 self.navigationController.interactivePopGestureRecognizer = <UIScreenEdgePanGestureRecognizer: 0x7fd6fce18490; state = Possible; delaysTouchesBegan = YES; view = <UILayoutContainerView 0x7fd6fcd088f0>; target= <(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition 0x7fd6fce18350>)>>
📍-[DemoViewController viewDidAppear:] + 65🎈 self.navigationController.interactivePopGestureRecognizer.delegate = <DemoViewController: 0x7fd6fcc132d0>
// ----> 直接设置 是不行🚫的
*/
iOS自带左侧滑动功能,但自定义leftBarButtonItem就导致侧滑失效,如下代码写于自定义导航栏控制器类中可恢复该功能:
- (void)viewDidLoad{
[super viewDidLoad];
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.delegate = self;
}
}
接着会出现一个bug,当你在根控制器的时候多次侧滑的时候,就是发现卡死的情况,那么要加上如下代理的代码,遵循代理UIGestureRecognizerDelegate.
//当手势开始滑动作用:拦截手势触发
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
//子控制器个数只剩下一个(这一个就是根控制器),手势不可用
BOOL open = self.childViewControllers.count != 1;
return open;
}
滑动返回