iOS 7 后退按钮弹出手势

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18876102/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:19:56  来源:igfitidea点击:

iOS 7 Back Button Pop Gesture

iphoneiosobjective-cuinavigationcontroller

提问by dlinsin

In iOS 7 there's the new swipe to pop gesture: You swipe from left to right on the left side of your screen and the UINavigationControllerpops back to the previous UIViewController.

在 iOS 7 中有新的滑动弹出手势:您在屏幕左侧从左向右滑动,UINavigationController 会弹回之前的UIViewController

When I create a custom back button like this, the swipe to pop gestures doesn't work anymore:

当我创建这样的自定义后退按钮时,滑动到弹出手势不再起作用:

UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStyleBordered target:self action:@selector(navigateBack)];
[customBackButton setBackButtonBackgroundImage:barBackBtnImg forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[customBackButton setBackButtonBackgroundImage:barBackBtnImgHighlighted forBarMetrics:UIBarMetricsDefault];
self.navigationItem.backBarButtonItem = customBackButton;

How can I use a custom back button and have the native swipe to pop gesture?

如何使用自定义后退按钮并使用本机滑动弹出手势?

Update:

更新:

That's what's happening in navigateBack:

这就是navigateBack 中发生的事情:

- (void)navigateBack {
    [self.navigationController popViewControllerAnimated:YES];
}

采纳答案by prizzl

Just add the following line of code:

只需添加以下代码行:

[self.navigationController.interactivePopGestureRecognizer addTarget:self action:@selector(handleGesture:)];

You can add your own UIGestureRecognizerand pop the UIViewControlleryourself. See the docsfor further info.

您可以添加自己的UIGestureRecognizer并自己弹出UIViewController。有关更多信息,请参阅文档

回答by udit gupta

There is no need to add your own gesture recognizer. The UINavigationController already does that for you. You need to set the delegate for the interactivePopGestureRecognizer before enabling it.

无需添加您自己的手势识别器。UINavigationController 已经为您做到了。您需要在启用之前设置 InteractivePopGestureRecognizer 的委托。

Do the following two things:

做以下两件事:

self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
[self.navigationController.interactivePopGestureRecognizer setEnabled:YES];

回答by Albert Chu

I use

我用

[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"nav_back.png"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"nav_back.png"]];

[UIBarButtonItem.appearance setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -64) forBarMetrics:UIBarMetricsDefault];

回答by Zeev Vax

To avoid crashes you have to be careful how you add and remove your custom back selector. The reason is that the navigation controller stays around while you pushing popping controller. As already stated after adding your custom back button+selector, you should do the following in viewDidApear.

为避免崩溃,您必须小心添加和删除自定义后退选择器的方式。原因是导航控制器在您推动弹出控制器时会留在周围。正如添加自定义后退按钮+选择器后所述,您应该在 viewDidApear 中执行以下操作。

    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
       self.navigationController.interactivePopGestureRecognizer.enabled = YES;
       self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
       [self.navigationController.interactivePopGestureRecognizer addTarget:self action:@selector(navigateBack)];
    }

Then in viewWillDisapear do

然后在 viewWillDisapear 做

        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
        {
           [self.navigationController.interactivePopGestureRecognizer removeTarget:self action:@selector(performCompletion)];
        }

The timing of these calls is a key. You can run into crashes otherwise, see more details on the reason in here

这些电话的时机是一个关键。否则您可能会遇到崩溃,请在此处查看有关原因的更多详细信息

回答by YuliaSh.

There is a new gesture recognizer UIScreenEdgePanGestureRecognizer. You can add it to your view and handle respectively (call navigateBack), replicating view controllers navigation behaviour.

有一个新的手势识别器UIScreenEdgePanGestureRecognizer。您可以将它分别添加到您的视图和句柄(调用navigateBack),复制视图控制器导航行为。

回答by Alcides Eduardo Zelaya

try adding this into the custom back button self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

尝试将此添加到自定义后退按钮 self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

回答by Jordan Montel

What did you do in "navigateBack" ?

你在“navigateBack”中做了什么?

Use this method like this :

像这样使用这个方法:

- (void)navigateBack
{
    [self.navigationController popViewControllerAnimated:YES];
}