博客> 开启使用系统自带的侧滑返回
开启使用系统自带的侧滑返回
2017-09-21 08:20 评论:0 阅读:70 lvhahaha
侧滑返回

开启使用系统自带的侧滑返回

iOS7之后系统提供了侧滑手势(interactivePopGestureRecognizer),即从屏幕左侧边缘滑起会pop回导航控制器栈的上个viewController。不过如果你自定义了UINavigationViewController或者自定义了返回按钮,系统自带的侧滑返回功能会失效。此时需要添加下面的代码解决:

首先创建一个baseVC ,所有的控制器都要继承与它。 @interface EPBaseViewController : UIViewController

//侧滑返回手势-- 可以做个开关

  • (void)enablePopGestureRecognizerDelegate:(BOOL)enable;

@end

在.m中实现其方法 @interface EPBaseViewController (){ id _popGestureRecognizerDelegate; }

  • (void)viewDidLoad {

    [super viewDidLoad];

    [self enablePopGestureRecognizerDelegate:YES];

}

  • (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];

}

  • (void)enablePopGestureRecognizerDelegate:(BOOL)enable { if (enable) { self.navigationController.interactivePopGestureRecognizer.delegate = _popGestureRecognizerDelegate; } else { if (self.navigationController.interactivePopGestureRecognizer.delegate ) { _popGestureRecognizerDelegate = self.navigationController.interactivePopGestureRecognizer.delegate; self.navigationController.interactivePopGestureRecognizer.delegate = nil; } } }

但是这样,在根视图控制器就会出现问题,如果有左滑的手势,整个app就卡掉了。所以在 UINavigationController 里边遵循UINavigationControllerDelegate代理 naviVC.delegate =self;

在下边设置代理方法就可以了 UINavationControllerDeleagate

  • (void)navigationController:(UINavigationController )navigationController didShowViewController:(UIViewController )viewController animated:(BOOL)animated { BOOL isRootVC = viewController == navigationController.viewControllers.firstObject; navigationController.interactivePopGestureRecognizer.enabled = !isRootVC; }
收藏
2
sina weixin mail 回到顶部