如何在 iOS 7 上的 UINavigationController 中禁用向后滑动手势
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17209468/
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 disable back swipe gesture in UINavigationController on iOS 7
提问by ArtFeel
In iOS 7 Apple added a new default navigation behavior. You can swipe from the left edge of the screen to go back on the navigation stack. But in my app, this behavior conflicts with my custom left menu. So, is it possible to disable this new gesture in UINavigationController?
在 iOS 7 中,Apple 添加了新的默认导航行为。您可以从屏幕左边缘滑动以返回导航堆栈。但是在我的应用程序中,这种行为与我的自定义左侧菜单冲突。那么,是否可以在 UINavigationController 中禁用这个新手势?
回答by ArtFeel
I found a solution:
我找到了一个解决方案:
Objective-C:
目标-C:
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
Swift 3+:self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
斯威夫特 3+:self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
回答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 Vladimir Samoylov
Just remove gesture recognizer from NavigationController. Work in iOS 8.
只需从 NavigationController 中删除手势识别器。在 iOS 8 中工作。
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
[self.navigationController.view removeGestureRecognizer:self.navigationController.interactivePopGestureRecognizer];
回答by Charlie Seligman
As of iOS 8 the accepted answer no longer works. I needed to stop the swipping to dismiss gesture on my main game screen so implemented this:
从 iOS 8 开始,接受的答案不再有效。我需要停止在我的主游戏屏幕上滑动以关闭手势,所以实现了这个:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return NO;
}
回答by Ja?ck
I've refined Twan's answer a bit, because:
我稍微改进了 Twan 的回答,因为:
- your view controller may be set as a delegate to other gesture recognisers
- setting the delegate to
nil
leads to hanging issues when you go back to the root view controller and make a swipe gesture before navigating elsewhere.
- 您的视图控制器可能被设置为其他手势识别器的委托
nil
当您返回到根视图控制器并在导航到其他地方之前做出滑动手势时,将委托设置为会导致挂起问题。
The following example assumes iOS 7:
以下示例假设为 iOS 7:
{
id savedGestureRecognizerDelegate;
}
- (void)viewWillAppear:(BOOL)animated
{
savedGestureRecognizerDelegate = self.navigationController.interactivePopGestureRecognizer.delegate;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
- (void)viewWillDisappear:(BOOL)animated
{
self.navigationController.interactivePopGestureRecognizer.delegate = savedGestureRecognizerDelegate;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer == self.navigationController.interactivePopGestureRecognizer) {
return NO;
}
// add whatever logic you would otherwise have
return YES;
}
回答by reza_khalafi
Please set this in root vc:
请在根 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;
}
回答by iPhone 7
For Swift:
对于斯威夫特:
navigationController!.interactivePopGestureRecognizer!.enabled = false
回答by Logic
it works for me in ios 10 and later :
它适用于 ios 10 及更高版本:
- (void)viewWillAppear:(BOOL)animated {
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
}
it doesnt work on viewDidLoad() method.
它不适用于 viewDidLoad() 方法。
回答by devxoul
EDIT
编辑
If you want to manage swipe back feature for specific navigation controllers, consider using SwipeBack.
如果您想管理特定导航控制器的向后滑动功能,请考虑使用SwipeBack。
With this, you can set navigationController.swipeBackEnabled = NO
.
有了这个,您可以设置navigationController.swipeBackEnabled = NO
.
For example:
例如:
#import <SwipeBack/SwipeBack.h>
- (void)viewWillAppear:(BOOL)animated
{
navigationController.swipeBackEnabled = NO;
}
It can be installed via CocoaPods.
它可以通过CocoaPods安装。
pod 'SwipeBack', '~> 1.0'
I appologize for lack of explanation.
我为缺乏解释而道歉。
回答by SoftDesigner
My method. One gesture recognizer to rule them all:
我的方法。一个手势识别器来统治他们:
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)
}
}
Important: don't reset the delegate anywhere in the navigation stack: navigationController!.interactivePopGestureRecognizer!.delegate = nil
重要提示:不要在导航堆栈中的任何位置重置委托: navigationController!.interactivePopGestureRecognizer!.delegate = nil