iOS 侧滑返回失败

一个偶然的事件,事情是这样的:
有一个页面需要侧滑返回拦截,我把代理设置给了当前的ViewController

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.interactivePopGestureRecognizer?.delegate = self
}
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    if let pan:UIPanGestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer{
        //做一些拦截操作
        return false
    }
    return true
}

然后我就发现其他页面就不能侧滑返回了,测试了好多次,只要进入这个ViewController之后,再去其他页面就不能侧滑了,再去其他ViewController之后打印self.navigationController?.interactivePopGestureRecognizer?.delegate为空,原来如此,那我就临时保存一下这个代理

private weak var tempDelete:UIGestureRecognizerDelegate?
override func viewDidLoad() {
      super.viewDidLoad()
      self.tempDelete = self.navigationController?.interactivePopGestureRecognizer?.delegate
      self.navigationController?.interactivePopGestureRecognizer?.delegate = self
}

然后我在viewWillDisappear中重新设置代理

override func viewWillDisappear(_ animated: Bool) {
  super.viewWillDisappear(animated)
  self.navigationController?.interactivePopGestureRecognizer?.delegate = tempDelete
}

然后就OK了,其他页面也可以侧滑了

你可能感兴趣的:(iOS 侧滑返回失败)