在 iOS 7 中更改返回按钮会禁用滑动导航返回

Changing back button in iOS 7 disables swipe to navigate back(在 iOS 7 中更改返回按钮会禁用滑动导航返回)

本文介绍了在 iOS 7 中更改返回按钮会禁用滑动导航返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 iOS 7 应用程序,我在其中设置了一个自定义后退按钮,如下所示:

I have an iOS 7 app where I am setting a custom back button like this:

    UIImage *backButtonImage = [UIImage imageNamed:@"back-button"];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [backButton setImage:backButtonImage forState:UIControlStateNormal];
    backButton.frame = CGRectMake(0, 0, 20, 20);

    [backButton addTarget:self
                   action:@selector(popViewController)
         forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    viewController.navigationItem.leftBarButtonItem = backBarButtonItem;

但这会禁用 iOS 7 的从左向右滑动"手势导航到上一个控制器.有谁知道我如何设置自定义按钮并仍然启用此手势?

But this disables the iOS 7 "swipe left to right" gesture to navigate to the previous controller. Does anyone know how I can set a custom button and still keep this gesture enabled?

我尝试改为设置 viewController.navigationItem.backBarButtonItem,但这似乎没有显示我的自定义图像.

I tried to set the viewController.navigationItem.backBarButtonItem instead, but this doesn't seem to show my custom image.

推荐答案

重要提示:这是一个黑客.我建议您看看这个答案.

IMPORTANT: This is a hack. I would recommend taking a look at this answer.

在分配 leftBarButtonItem 后调用以下行对我有用:

Calling the following line after assigning the leftBarButtonItem worked for me:

self.navigationController.interactivePopGestureRecognizer.delegate = self;

如果在 init 方法中调用,这将不起作用.它应该在 viewDidLoad 或类似方法中调用.

This does not work if called in init methods. It should be called in viewDidLoad or similar methods.

这篇关于在 iOS 7 中更改返回按钮会禁用滑动导航返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 iOS 7 中更改返回按钮会禁用滑动导航返回

基础教程推荐