简体   繁体   English

UINavigationController类别具有自定义后退按钮的Pop手势

[英]UINavigationController Category Pop Gesture with Custom Back Button

I use a custom back button in tons of places in an app I'm working on, so I have a category on UINavigationController that adds a pushViewControllerWithBackArrow method that sets a custom back arrow on the view controller being pushed, then pushes it. 我在一个正在处理的应用程序中的很多地方都使用了一个自定义的后退按钮,因此我在UINavigationController上有一个类别,该类别添加了pushViewControllerWithBackArrow方法,该方法在要按下的视图控制器上设置自定义后退箭头,然后再将其按下。 However, I can't seem to get the interactivePopGestureRecognizer working. 但是,我似乎无法使interactivePopGestureRecognizer正常工作。 The code I've found here on stack overflow and online all either subclass UINavigationController or set the interactivePopGestureRecognizer in the pushed view controller. 我在堆栈上发现的代码溢出并在线所有子类UINavigationController或在推入的视图控制器中设置了interactivePopGestureRecognizer

One solution ( http://keighl.com/post/ios7-interactive-pop-gesture-custom-back-button/ ) involved adding this code to viewDidLoad in the UINavigationController subclass: 一种解决方案( http://keighl.com/post/ios7-interactive-pop-gesture-custom-back-button/ )将以下代码添加到UINavigationController子类的viewDidLoad中:

__weak UINavigationController *weakSelf = self;

if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
{
    self.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)weakSelf;
}

which was semi-functional, but if I swiped back when in my root view controller it locked up the UI. 这是半功能的,但是如果我在根视图控制器中向后滑动,则会锁定UI。 The solution to this problem involved subclassing UINavigationController and overriding pushViewController and didShowViewController, which I would like to avoid. 解决此问题的方法涉及子类化UINavigationController并重写pushViewController和didShowViewController,我想避免这种情况。

Is there any way to make the gesture recognizer work on a UINavigationController category? 有什么方法可以使手势识别器在UINavigationController类别上工作?

The best solution is to create a category for a UIViewController so that you could use this throughout your project as shown below. 最好的解决方案是为UIViewController创建一个类别,以便您可以在整个项目中使用它,如下所示。 You can also just copy the code for the method customizeBackButton and put it in whatever view controller you need 您也可以只复制方法customBackButton的代码并将其放入所需的任何视图控制器中

//UIViewController+Back.h
#import <UIKit/UIKit.h>

@interface UIViewController (Back)

- (void)customizeBackButton;


@end

implement this method 实现这个方法

//UIViewController+Back.m
#import "UIViewController+Back.h"

@implementation UIViewController (BackButton)
- (void)customizeBackButton
{
    UIImage *backButtonImage = [UIImage imageNamed:@"back.png"];
    UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:self.navigationController action:@selector(popViewControllerAnimated:)];
    self.navigationItem.hidesBackButton = YES;
    [self.navigationItem setLeftBarButtonItem: customItem];
}
@end

Please note that you have to hide the original back button with self.navigation.hidesBackButton or else they'll both show up. 请注意,您必须使用self.navigation.hidesBackButton隐藏原始的后退按钮,否则它们都会同时显示。 Then, use it in wherever you need it! 然后,在需要的地方使用它!

-(void) viewWillAppear:(BOOL)animated {
    [self customizeBackButton];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM