search

Home  >  Q&A  >  body text

objective-c - How to disable the side right swipe pop gesture of the current controller? Currently, VC is pushed here.

For example: My heel controller is a navigation controller, and now there is a controller A that I pushed over. I hope that this controller can only pop by clicking the return button on the left side of the navigation bar, and slide right on the side. I hope to disable the pop gesture. Of course, I hope other controllers don’t still have this pop gesture. Please tell me how to implement this change?

習慣沉默習慣沉默2793 days ago584

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-05-02 09:31:05

    Write in controller A:

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
    
        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
    
    // 给该控制器添加协议 <UIGestureRecognizerDelegate>
    
    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
    {
        return gestureRecognizer != self.navigationController.interactivePopGestureRecognizer;
    }
    

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-02 09:31:05

    If the construction from A -> B is more elegant

    Inside B’s viewDidLoad

    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
        [super pushViewController:viewController animated:animated];
        self.interactivePopGestureRecognizer.enabled = NO;
    }

    Then inside viewDidDisappear

    self.navigationController.interactivePopGestureRecognizer.enabled = YES;

    It’s all written in B, isn’t it much more elegant

    reply
    0
  • Cancelreply