swift版:
- class xxxxxViewController: UIViewController {
- // 修复侧滑丢失
- private var naDelegate: UIGestureRecognizerDelegate?
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- // 修复侧滑丢失
- naDelegate = navigationController?.interactivePopGestureRecognizer?.delegate
- navigationController?.interactivePopGestureRecognizer?.delegate = self as? UIGestureRecognizerDelegate
- }
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- // 修复侧滑丢失
- navigationController?.interactivePopGestureRecognizer?.delegate = naDelegate
- }
iOS版(未测试):
在某个控制器里一旦修改了navigation 的 leftBarButtonItem, 像这样
self.navigationItem.leftBarButtonItem = leftBarButton;
就会导致滑动返回手势失效, 原因是修改 leftBarButtonItem 导致手势代理丢失, 解决办法如下:
在进入页面时 viewWillAppear 中保存代理对象到全局变量 nvDelegate 中
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// 修改了 navigation 的 leftbarburron 导致滑动返回丢失, 需重新设置代理, 但根控制器不能有滑动返回手势
self.nvDelegate = self.navigationController.interactivePopGestureRecognizer.delegate;
self.navigationController.interactivePopGestureRecognizer.delegate = self; // 重新设置代理
}
在离开页面时, 把 nvDelegate 还原回 navigation 中, 叫做恢复现场
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// 修改了 navigation 的 leftbarburron 导致滑动返回丢失, 需重新设置代理, 但根控制器不能有滑动返回手势
self.navigationController.interactivePopGestureRecognizer.delegate = self.nvDelegate;
}