在iOS 7中,苹果增加了一个新的默认导航行为。你可以从屏幕的左边缘滑动回到导航堆栈。但在我的应用程序中,这种行为与我的自定义左侧菜单冲突。那么,是否有可能在UINavigationController中禁用这个新手势?
当前回答
请在root vc下设置:
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:YES];
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
其他回答
它适用于我的大多数视图控制器。
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
对于一些视图控制器,比如UIPageViewController,它并不是无效的。在UIPageViewController的pagecontentviewcontroller下面的代码为我工作。
override func viewDidLoad() {
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
self.navigationController?.interactivePopGestureRecognizer?.delegate = self
}
override func viewWillDisappear(_ animated: Bool) {
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
self.navigationController?.interactivePopGestureRecognizer?.delegate = nil
}
On UIGestureRecognizerDelegate,
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == self.navigationController?.interactivePopGestureRecognizer {
return false
}
return true
}
我找到了一个解决方案:
objective - c:
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
斯威夫特3 +: self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
没有一个给出的答案帮助我解决这个问题。在这里张贴我的答案;可能对某人有帮助
声明私有var popGesture: UIGestureRecognizer?作为视图控制器中的全局变量。然后在viewDidAppear和viewWillDisappear方法中实现代码
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if self.navigationController!.respondsToSelector(Selector("interactivePopGestureRecognizer")) {
self.popGesture = navigationController!.interactivePopGestureRecognizer
self.navigationController!.view.removeGestureRecognizer(navigationController!.interactivePopGestureRecognizer!)
}
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
if self.popGesture != nil {
navigationController!.view.addGestureRecognizer(self.popGesture!)
}
}
这将禁用滑动回iosv8。x起
所有这些解决方案都以一种他们不推荐的方式操纵苹果的手势识别器。我的一个朋友刚刚告诉我,有一个更好的解决方案:
[navigationController.interactivePopGestureRecognizer requireGestureRecognizerToFail: myPanGestureRecognizer];
其中myPanGestureRecognizer是你用来显示菜单的手势识别器。这样,当你按下一个新的导航控制器时,苹果的手势识别器就不会被重新打开,也不需要依赖黑客延迟,如果你的手机处于睡眠状态或负载过重,可能会过早触发。
把这个留在这里,因为我知道下次需要的时候我不会记得了,然后我就有了这个问题的解决方案。
我的方法。一个手势识别器来统治它们:
class DisabledGestureViewController: UIViewController: UIGestureRecognizerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
navigationController!.interactivePopGestureRecognizer!.delegate = self
}
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
// Prevent going back to the previous view
return !(navigationController!.topViewController is DisabledGestureViewController)
}
}
重要:不要在导航堆栈的任何地方重置委托:navigationController!.interactivePopGestureRecognizer!.delegate = nil
推荐文章
- iPhone上UIView和UILabels的渐变
- keychain上的分发证书中缺少私钥
- 在实现API时,我如何避免在块中捕获自我?
- 如何创建一个Swift Date对象?
- Xcode 4在目标设备上说“finished running <my app>”——什么都没有发生
- 从另一个应用程序打开设置应用程序
- 快速提取正则表达式匹配
- 如何应用梯度的背景视图的iOS Swift应用程序
- 图书馆吗?静态的?动态吗?或框架?另一个项目中的项目
- 如何用SwiftUI调整图像大小?
- Xcode 6 gitignore文件应该包括什么?
- 如何在iPhone/iOS上删除电话号码的蓝色样式?
- 检测视网膜显示
- 如何在UIImageView中动画图像的变化?
- 如何从iPhone访问SOAP服务