关于 ios 7.0启用的UINavgationViewController的interactivePopGestureRecognizer手势

相信所有试着使用自定义返回的leftItem,但是也会发现一个问题就是ios7.0的那个pan返回上一页的手势失效了。

因为自己使用app的时候是7.0的系统,所以呢,觉得相当的不爽,于是纠结了,然后到stackoverflow.com里面找到相关的资料,但是各种try,各种bug,最后,综合所有的方法。

嗯,好吧,最后还是采用网上的一个放,继承UINavgationViewController,然后派生对应方法。

于是,使用这个代码在你的工程里就OK,暂时未发现BUG。标记:

  CBNavigationController.h

#import <UIKit/UIKit.h>

 

@interface CBNavigationController : UINavigationController <UINavigationControllerDelegate, UIGestureRecognizerDelegate>

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;

 

@end

 

#import "CBNavigationController.h"

 

 

   CBNavigationController.m

@implementation CBNavigationController

 

- (void)viewDidLoad

{

    __weakCBNavigationController *weakSelf = self;

    

    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])

    {

        self.interactivePopGestureRecognizer.delegate = weakSelf;

        self.delegate = weakSelf;

    }

}

 

// Hijack the push method to disable the gesture

 

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated

{

    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]&&animated==YES)

        self.interactivePopGestureRecognizer.enabled = NO;

    

    [super pushViewController:viewController animated:animated];

}

 

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated

{

    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]&&animated==YES)

        self.interactivePopGestureRecognizer.enabled = NO;

    

   return  [superpopToRootViewControllerAnimated:animated];

}

 

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated

{

    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])

        self.interactivePopGestureRecognizer.enabled = NO;

    return [super popToViewController:viewController animated:animated];

}

#pragma mark UINavigationControllerDelegate

 

- (void)navigationController:(UINavigationController *)navigationController

       didShowViewController:(UIViewController *)viewController

                    animated:(BOOL)animate

{

    // Enable the gesture again once the new controller is shown

    

    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]){

        self.interactivePopGestureRecognizer.enabled = YES;

    }

}

 

 

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

{

    if (gestureRecognizer==self.interactivePopGestureRecognizer) {

        if (self.viewControllers.count<2||self.visibleViewController==[self.viewControllersobjectAtIndex:0]) {

            return NO;

        }

    }

    returnYES;

}

 

@end

 

 

原文地址:https://www.cnblogs.com/lingzhiguiji/p/3646028.html