UI

iOS 某个特定页面禁用侧滑返回手势,其他页面正常

2018-12-20  本文已影响0人  jksniper

A->B->C,

B需要禁用侧滑返回,A和C正常(默认支持侧滑返回)。A、B、C的基类都是BaseVC。

1、在BaseVC里设置一个属性:

@property (nonatomic, assign) BOOL DisableSidePop;//禁止侧滑返回

在.m文件里写set方法

- (void)setDisableSidePop:(BOOL)DisableSidePop{
    _DisableSidePop = DisableSidePop;
    if (DisableSidePop) {
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

            self.navigationController.interactivePopGestureRecognizer.delegate = self;
        }
    }else{
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

            self.navigationController.interactivePopGestureRecognizer.delegate = nil;
        }
    }
}

加上手势代理方法

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    return !self.DisableSidePop;
}

也可以在dealloc里设置手势代理失效,以防万一。

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    if (self.DisableSidePop) {
        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
}

2、使用:在A页面要Push B的时候写

BVC *vc = [[BVC alloc]init];
self.DisableSidePop = NO;
vc.DisableSidePop = YES;
[self.navigationController pushViewController:vc animated:YES];

3、结束。

此操作是在iOS12环境下,其他环境不知道是否有效

上一篇下一篇

猜你喜欢

热点阅读