ios 如何关闭自动手势以使用导航控制器返回视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19019900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to turn off the automatic gesture to go back a view with a navigation controller?
提问by Danoli3
So I'm noticing all of my views are receiving the gesture to go back (pop a view) when the user swipes on the very left side of the screen (in either orientation) (This is new with iOS7)
所以我注意到当用户在屏幕的最左侧(以任一方向)滑动时,我的所有视图都收到返回(弹出视图)的手势(这是 iOS7 的新功能)
I've tried so far with no avail to turn it off using:
到目前为止,我已经尝试过使用以下方法关闭它无济于事:
[self.navigationItem setHidesBackButton:YES];
Within the init of the NavigationController itself (as the delegate seems to be using that).
在 NavigationController 本身的 init 中(因为委托似乎正在使用它)。
回答by Gabriele Petronella
obj-c
对象-c
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
swift
迅速
navigationController?.interactivePopGestureRecognizer?.isEnabled = false
回答by Danoli3
Adding to Gabriele's Solution.
添加到 Gabriele 的解决方案中。
To support any iOS before iOS 7 you will need to wrap this code with this:
要支持 iOS 7 之前的任何 iOS,您需要使用以下代码包装此代码:
if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
This will stop the App crashing in iOS 6 and iOS 5 for missing selector.
这将阻止应用程序在 iOS 6 和 iOS 5 中因缺少选择器而崩溃。
回答by Userich
I use this solution in my project, it disables only interactivePopGestureRecognizer and allows you to use another gesture recognizers.
我在我的项目中使用此解决方案,它仅禁用 InteractivePopGestureRecognizer 并允许您使用其他手势识别器。
- (void)viewDidLoad {
[super viewDidLoad];
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if ([gestureRecognizer isEqual:self.navigationController.interactivePopGestureRecognizer]) {
return NO;
} else {
return YES;
}
}
回答by Antoine
I found out setting the gesture to disabled only doesn't always work. It does work, but for me it only did after I once used the backgesture. Second time it wouldn't trigger the backgesture.
我发现将手势设置为仅禁用并不总是有效。它确实有效,但对我来说只有在我使用过 backgesture 之后才有效。第二次它不会触发backgesture。
Fix for me was to delegate the gesture and implement the shouldbegin method to return NO:
对我来说修复是委托手势并实现 shouldbegin 方法以返回 NO:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Disable iOS 7 back gesture
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// Enable iOS 7 back gesture
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return NO;
}
回答by ilan
For IOS 8 (Swift):
对于 IOS 8 (Swift):
class MainNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
self.interactivePopGestureRecognizer.enabled = false
// Do any additional setup after loading the view.
}
}
回答by Shardul
Use this code for previous than iOS 7
在 iOS 7 之前使用此代码
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}