美文网首页iOS 技能
iOS 关闭系统左滑动返回

iOS 关闭系统左滑动返回

作者: sttech | 来源:发表于2017-10-12 11:04 被阅读0次

    禁用滑动返回手势需要在改界面的ViewController中添加如下代码

    - (void)viewDidAppear:(BOOL)animated  
    {  
        [super viewDidAppear:animated];  
        // 禁用返回手势  
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {  
            self.navigationController.interactivePopGestureRecognizer.enabled = NO;  
        }  
    } 
    

    如果只是该界面禁用滑动返回手势,还需要添加如下代码使其他界面能够继续使用滑动返回手势:

    - (void)viewWillDisappear:(BOOL)animated  
    {  
        [super viewWillDisappear:animated];  
        // 开启返回手势  
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {  
            self.navigationController.interactivePopGestureRecognizer.enabled = YES;  
        }  
    }  
    

    如果以上都不可以的话 可以采用 一下方法

    • 方法一
        id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
        UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
        [self.view addGestureRecognizer:pan];
    
    • 方法二 需要实现UIGestureRecognizerDelegate的代理
    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.navigationController.interactivePopGestureRecognizer.delegate = self;
        }
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.navigationController.interactivePopGestureRecognizer.delegate = nil;
        }
        
    }
    
    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
    {
        return NO;
    }
    

    相关文章

      网友评论

        本文标题:iOS 关闭系统左滑动返回

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