美文网首页
iOS屏幕左侧向右滑动返回(自定义返回)

iOS屏幕左侧向右滑动返回(自定义返回)

作者: 762683ff5d3d | 来源:发表于2017-07-03 14:42 被阅读0次

    首先,你的Vc是有NavigationController来管理的,就是得有导航栏.

    这个人性化的便捷操作从iOS7开始.

    为什么有些ViewController可以左侧滑动返回,有些页面这个手势就不起作用了?

    手势可用:当前页面的返回(pop)事件没有被自定义,使用的是系统的back,interactivePopGestureRecognizer.enable =YES ,默认是开启的状态,看名字也知道Ta干什么的.

    手势失效: 当自定义了返回的图标样式,文案等信息后,手势的值就变了 → → →interactivePopGestureRecognizer.enable = NO

    针对上面的(2)问题,来看看怎么在次开启这个功能

    这个功能,就是NavigationController中的interactivePopGestureRecognizer属性,原理在上面已经介绍的差不多了,只是一个属性.

    手动开启要注意这个点

    1. 如果手动开启了interactivePopGestureRecognizer,在根Vc中要把Ta设置成NO,不然滑动依旧生效,但是已经没有上级Vc,找不到就会导致程序Crash.

    手动开启的思路

    开启功能:要在根VC的viewWillDisAppear方法中设置当前的NavigationController.的interactivePopGestureRecognizer.enable =YES;

    根VC的viewDidAppear等方法中设置interactivePopGestureRecognizer.enable =NO;

    开启代码如下:

    -(void)viewWillDisappear:(BOOL)animated

    {

       [super viewWillDisappear:animated];

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

           self.navigationController.interactivePopGestureRecognizer.enabled = YES;

       }

    }

    关于上文提到的注意点

    - (void)viewDidAppear:(BOOL)animated

    {

       [super viewDidAppear:animated];

    //禁用返回手势

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

           self.navigationController.interactivePopGestureRecognizer.enabled = NO;

    }

    相关文章

      网友评论

          本文标题:iOS屏幕左侧向右滑动返回(自定义返回)

          本文链接:https://www.haomeiwen.com/subject/beaacxtx.html