簡體   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