繁体   English   中英

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

[英]UINavigationController Category Pop Gesture with Custom Back Button

我在一个正在处理的应用程序中的很多地方都使用了一个自定义的后退按钮,因此我在UINavigationController上有一个类别,该类别添加了pushViewControllerWithBackArrow方法,该方法在要按下的视图控制器上设置自定义后退箭头,然后再将其按下。 但是,我似乎无法使interactivePopGestureRecognizer正常工作。 我在堆栈上发现的代码溢出并在线所有子类UINavigationController或在推入的视图控制器中设置了interactivePopGestureRecognizer

一种解决方案( 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;
}

这是半功能的,但是如果我在根视图控制器中向后滑动,则会锁定UI。 解决此问题的方法涉及子类化UINavigationController并重写pushViewController和didShowViewController,我想避免这种情况。

有什么方法可以使手势识别器在UINavigationController类别上工作?

最好的解决方案是为UIViewController创建一个类别,以便您可以在整个项目中使用它,如下所示。 您也可以只复制方法customBackButton的代码并将其放入所需的任何视图控制器中

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

@interface UIViewController (Back)

- (void)customizeBackButton;


@end

实现这个方法

//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

请注意,您必须使用self.navigation.hidesBackButton隐藏原始的后退按钮,否则它们都会同时显示。 然后,在需要的地方使用它!

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

暂无
暂无

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

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