解决Custom backBarButtonItem 后 interactivePopGestureRecognizer失效的问题

注:  它这种方法写的貌似不全面,如果多次快速操作push pop操作后可能会导致页面卡顿的严重Bug,可看下一篇文章,那个暂时未发现问题


转载自:http://www.devdiv.com/custom_backbarbuttonitem_interactivepopgesturerecognizer_-blog-37464-52866.html


最近自己在做项目的时候,要将NavigationItem的BackBarButtonItem修改为自定义的图片。当成功修改以后发现无法实现Navigation的滑动回退功能。通过在StackOverFlow查找以后,得到以下完美解决方案。

步骤一:
首先Custom一个UINavigationConroller.

WLNavigationControlller :  UINavigationController <UIGestureRecognizerDelegate,UINavigationControllerDelegate>


步骤二:
重构 viewDidLoad方法
- ( void )viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    __weak WLNavigationControlller *weakSelf = self;

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

    {

        self.interactivePopGestureRecognizer.delegate = weakSelf;

        self.delegate = weakSelf;

    }

}

步骤三:

重构pushViewController方法

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

{

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

        self.interactivePopGestureRecognizer.enabled = NO;

       [super pushViewController:viewController animated:animated];

}

步骤四:

实现delegate方法

- (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;

}

如此四步,就可以解决手势返回的问题,且不会出现freeze住UI的bug。

我分析认为UIViewController应该实现了UIGestureRecongnizer的target action,并实现了delegate方法,当人为修改了BackBarButtonItem属性以后,interactivePopGestureRecognizer的delegate为nil,导致手势功能失效(我认为这里是通过delegate来实现pop功能),所以在viewDidLoad里要给interactivePopGestureRecognizer设置delegate属性,但并不用具体实现delegate方法。在push和push成功以后设置enabled属性我猜想是因为在手势的过程中一些事件没处理好,导致页面无法响应事件,出现假死的状况。不知到这个是不是苹果的bug。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值