iOS禁用右滑返回的三种方式

方式一:

前提:如果使用的自定义UINavigationController基类,请不要在此基类里写相关的手势操作方法。

代码如下:
- (void)viewDidAppear:(BOOL)animated {
    
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
}

方式二:

流程:先设置代理---->重写手势操作方法
代码如下:
- (void)viewDidAppear:(BOOL)animated {
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    
    //YES:允许右滑返回  NO:禁止右滑返回
    return NO;
    
}

如果以上两种方法尝试过没有用的情况下推荐第三种方法来解决:

代码如下:
- (void)viewWillAppear:(BOOL)animated {
    
    //禁止返回
    id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
    [self.view addGestureRecognizer:pan];
    
}

直接重写手势的代理方法来实现,直接拷贝代码就行

你可能感兴趣的:(iOS禁用右滑返回的三种方式)