UINavigationController返回手势失效问题
问题描述
- 从iOS7开始,系统为UINavigationController提供了一个interactivePopGestureRecognizer用于右滑返回(pop),但是,如果自定了返回按钮或者隐藏了navigationBar,该手势就失效了。
原因
- 自定义返回按钮或者隐藏navigationBar时,interactivePopGestureRecognizer的delegate被阻断事件传递。
解决方案
- 自定义UINavigationController,重写interactivePopGestureRecognizer的delegate相关方法。
- 代码如下:
@interface BaseNavigationController () <UIGestureRecognizerDelegate>
@end
@implementation BaseNavigationController
-(void)viewDidLoad {
[super viewDidLoad];
self.interactivePopGestureRecognizer.delegate = self;
}
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
return self.viewControllers.count > 1;
}
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
[super pushViewController:viewController animated:animated];
self.interactivePopGestureRecognizer.enabled = NO;
}
@end
第三方的GitHub链接