如何禁用仅在一个视图控制器上专门向后滑动

How to disable swipe back specifically on only one view controller

我知道有人问过类似的问题,但是 none 对我有用。

我有这个代码可以在我的项目中启用回扫

    class InteractivePopRecognizer: NSObject {

    // MARK: - Properties

    fileprivate weak var navigationController: UINavigationController?

    // MARK: - Init

    init(controller: UINavigationController) {
        self.navigationController = controller

        super.init()

        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
    }
}

extension InteractivePopRecognizer: UIGestureRecognizerDelegate {
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return (navigationController?.viewControllers.count ?? 0) > 1
    }

    // This is necessary because without it, subviews of your top controller can cancel out your gesture recognizer on the edge.
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

我有这个VC堆栈

主屏幕VC -> Login/SignupVC -> 用户配置文件VC

我不希望他们能够从 UserProfileVC 滑动回来。

更好的方法是在显示 UserProfileVC

时将它们从堆栈中清除
let profile  = self.storyboard?.instantiateViewController(withIdentifier: "profileID") as! UserProfileVC
self.navigationController?.viewControllers = [profile]

编辑: 在 profileVC 中执行此操作

self.navigationController?.viewControllers = [self]

//

self.view.alpha = 0

UIView.animate(withDuration: 0.5) {

    self.view.alpha = 1

}

我认为您也可以从那里删除手势识别器,为什么您不尝试这样做。 尝试这样的事情:-

view.gestureRecognizers?.removeAll()