我知道有人问过类似的问题,但没有一个对我有用。
我有这段代码可以在我的项目中启用向后滑动
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 堆栈
HomescreenVC -> Login/SignupVC -> UserProfileVC
我不希望他们能够从 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
}
关于swift - 如何禁用仅在一个 View Controller 上专门向后滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51631162/