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

后端 未结 6 1038
清歌不尽
清歌不尽 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:30

    obj-c

    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    

    swift

    navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    
    0 讨论(0)
  • 2020-12-13 07:35

    For IOS 8 (Swift):

    class MainNavigationController: UINavigationController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.interactivePopGestureRecognizer.enabled = false
    
            // Do any additional setup after loading the view.
        }
    
    }
    
    0 讨论(0)
  • 2020-12-13 07:35

    Use this code for previous than iOS 7

    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
    
    0 讨论(0)
  • 2020-12-13 07:38

    Adding to Gabriele's Solution.

    To support any iOS before iOS 7 you will need to wrap this code with this:

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

    This will stop the App crashing in iOS 6 and iOS 5 for missing selector.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-12-13 07:46

    I use this solution in my project, it disables only interactivePopGestureRecognizer and allows you to use another gesture recognizers.

    - (void)viewDidLoad {
    
        [super viewDidLoad];
    
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    
            self.navigationController.interactivePopGestureRecognizer.enabled = NO;
            self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
        }
    
    }
    
    
    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    
        if ([gestureRecognizer isEqual:self.navigationController.interactivePopGestureRecognizer]) {
    
            return NO;
    
        } else {
    
            return YES;
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题