IOS:禁止页面上的侧滑返回手势
建立UIViewController的分类,加上如下两个方法
@implementation UIViewController (UIViewC) + (void)popGestureClose:(UIViewController *)VC { // 禁用侧滑返回手势 if ([VC.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { //这里对添加到右滑视图上的所有手势禁用 for (UIGestureRecognizer *popGesture in VC.navigationController.interactivePopGestureRecognizer.view.gestureRecognizers) { popGesture.enabled = NO; } //若开启全屏右滑,不能再使用下面方法,请对数组进行处理 //VC.navigationController.interactivePopGestureRecognizer.enabled = NO; } } + (void)popGestureOpen:(UIViewController *)VC { // 启用侧滑返回手势 if ([VC.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { //这里对添加到右滑视图上的所有手势启用 for (UIGestureRecognizer *popGesture in VC.navigationController.interactivePopGestureRecognizer.view.gestureRecognizers) { popGesture.enabled = YES; } //若开启全屏右滑,不能再使用下面方法,请对数组进行处理 //VC.navigationController.interactivePopGestureRecognizer.enabled = YES; } }
然后在页面上进行方法的开启与关闭
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [UIViewController popGestureClose:self]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [UIViewController popGestureOpen:self]; }
这个离开时开启手势,如果没有生效,可以在离开后的第一个界面导入分类,然后在类一开始加上开启手势的语句。