我们知道,iOS7以后,导航控制器默认带了侧滑返回功能,但是仅限于屏幕边缘。而且在你自定义leftBarButtonItem等之后侧滑效果就会消失。这种问题怎么解决呢?
首先,我们先来看看系统的这种手势是什么?
NSLog(@"%@",self.interactivePopGestureRecognizer);//这里的self是项目中用的navigationController
打印结果:
<UIScreenEdgePanGestureRecognizer: 0x15ecc9e0; state = Possible; delaysTouchesBegan = YES; view = <UILayoutContainerView 0x15da65a0>; target= <(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition 0x15ecc900>)>>
解决办法:自定义一个滑动手势,当手势触发时,调用系统手势的target的相应方法。经过验证,这个target就是interactivePopGestureRecognizer.delegate,方法名是:handleNavigationTransition:
上代码:
id taget = self.interactivePopGestureRecognizer.delegate; UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:taget action:@selector(handleNavigationTransition:)]; [self.view addGestureRecognizer:pan]; pan.delegate = self; //因为根控制器不需要滑动返回,所以要通过代理方法处理一下 self.interactivePopGestureRecognizer.enabled = NO; //这句不能忘啊!!!!关掉系统手势
//手势代理方法
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if (self.childViewControllers.count == 1) {
return NO;
}
return YES;
}
参考资料
http://www.cocoachina.com/ios/20150401/11459.html
http://www.jianshu.com/p/737924684695
http://www.jianshu.com/p/c0b7a97b3b0e