1.首先需要自定义navigationController继承UINavigationController,让它自己成为interactivePopGestureRecognizer的代理
@interface NavViewController ()<UINavigationControllerDelegate,UIGestureRecognizerDelegate>
@end
@implementation NavViewController
- (void)viewDidLoad {
[super viewDidLoad];
__weak typeof(self) weakself = self;
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer )]) {
self.interactivePopGestureRecognizer.delegate = weakself;
}
}
2. 为了解决在push的时候与后退手势在成堆栈混乱导致崩溃的问题,需要在push的时候禁用interactivePopGestureRecognizer,在新的视图控制器加载完成后再启用interactivePopGestureRecognizer
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.enabled = NO;
[super pushViewController:viewController animated:animated];
}
}
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
if (navigationController.viewControllers.count ==1) {
navigationController.interactivePopGestureRecognizer.enabled = NO;
}else
{
navigationController.interactivePopGestureRecognizer.enabled = YES;
}
}
}
3. 这样navigationController自定义完成,要实现想要效果只需要在自定义BackItem的控制器中去设置interactivePopGestureRecognizer便可OK