年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2450|回复: 0

iOS7 NavigationController 手势问题

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-7-28 09:17:06 | 显示全部楼层 |阅读模式
    在iOS7中,如果使用了UINavigationController,那么系统自带的附加了一个从屏幕左边缘开始滑动可以实现pop的手势。但是,如果自定义了navigationItem的leftBarButtonItem,那么这个手势就会失效。解决方法有很多种

    1.重新设置手势的delegate


    self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;


    2.当然你也可以自己响应这个手势的事件

    [self.navigationController.interactivePopGestureRecognizer addTarget:self action:@selector(handleGesture:)];
      
      我使用了第一种方案,继承UINavigationController写了一个子类,直接设置delegate到self。最初的版本我是让这个gesture始终可以begin
    1. -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
    2. {
    3. return YES;
    4. }
    复制代码
      
      但是出现很多问题,比如说在rootViewController的时候这个手势也可以响应,导致整个程序页面不响应;如果在push的同时我触发这个手势,那么会导致navigationBar错乱,甚至crash;push了多层后,快速的触发两次手势,也会错乱。尝试了种种方案后我的解决方案是加个判断,代码如下,这次运行良好了。
    1. @interface NavRootViewController : UINavigationController<UIGestureRecognizerDelegate,UINavigationControllerDelegate>
    2. @property(nonatomic,weak) UIViewController* currentShowVC;
    3. @end
    4. @implementation NavRootViewController
    5. -(id)initWithRootViewController:(UIViewController *)rootViewController
    6. {
    7. NavRootViewController* nvc = [super initWithRootViewController:rootViewController];
    8. self.interactivePopGestureRecognizer.delegate = self;
    9. nvc.delegate = self;
    10. return nvc;
    11. }
    12. -(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
    13. {
    14. }
    15. -(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
    16. {
    17. if (navigationController.viewControllers.count == 1)
    18. self.currentShowVC = Nil;
    19. else
    20. self.currentShowVC = viewController;
    21. }
    22. -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
    23. {
    24. if (gestureRecognizer == self.interactivePopGestureRecognizer) {
    25. return (self.currentShowVC == self.topViewController);
    26. }
    27. return YES;
    28. }
    29. @end
    复制代码
      正当窃喜的时候,发现了另一个更高端的解决方案,就是参考2。使用backBarButtonItem
    1. // 统一替换 back item 的图片
    2. [self.navigationController.navigationBar setBackIndicatorImage:
    3. [UIImage imageNamed:@"CustomerBackImage"]];
    4. [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:
    5. [UIImage imageNamed:@"CustomerBackImage"]];
    6. // back item的标题 是 被将要返回的页面 所控制的,所以如果要改变当前的back item的标题,要在上一个viewcontroller里面设置
    7. self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    复制代码
      
      不过这种方案的缺点是如果你要设置 leftBarButtonItems 来更多的自定义左边的 items时,手势又会失效(或者有其他方案我还不知道*—*)。所以最后我又选择了自己的方法。


    参考链接:http://www.2cto.com/kf/201401/272886.html
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-1 12:04 , Processed in 0.045015 second(s), 19 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表