How to turn off the automatic gesture to go back a view with a navigation controller?

后端 未结 6 1040
清歌不尽
清歌不尽 2020-12-13 06:56

So I\'m noticing all of my views are receiving the gesture to go back (pop a view) when the user swipes on the very left side of the screen (in either orientation) (

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 07:39

    I found out setting the gesture to disabled only doesn't always work. It does work, but for me it only did after I once used the backgesture. Second time it wouldn't trigger the backgesture.

    Fix for me was to delegate the gesture and implement the shouldbegin method to return NO:

    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    
        // Disable iOS 7 back gesture
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.navigationController.interactivePopGestureRecognizer.enabled = NO;
            self.navigationController.interactivePopGestureRecognizer.delegate = self;
        }
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
    
        // Enable iOS 7 back gesture
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.navigationController.interactivePopGestureRecognizer.enabled = YES;
            self.navigationController.interactivePopGestureRecognizer.delegate = nil;
        }
    }
    
    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
    {
        return NO;
    }
    

提交回复
热议问题