美文网首页
iOS 侧滑返回失败

iOS 侧滑返回失败

作者: XC1988 | 来源:发表于2022-08-26 17:51 被阅读0次

    一个偶然的事件,事情是这样的:
    有一个页面需要侧滑返回拦截,我把代理设置给了当前的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 侧滑返回失败

          本文链接:https://www.haomeiwen.com/subject/fsvfnrtx.html