4

I want to disable the pop gesture on swiping the viewcontroller but the below lines are not working in iOS 8:

self.navigationController.interactivePopGestureRecognizer.enabled = NO;

Thanks in Advance

Dhara
  • 4,093
  • 2
  • 36
  • 69

4 Answers4

5

In the viewcontroller that you want it to be disabled, add the following line:

- (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;
}
gabbler
  • 13,626
  • 4
  • 32
  • 44
  • Are you sure about that? As I tested the above code in iOS 8 and it worked. – gabbler Sep 29 '14 at 12:30
  • self.navigationController.interactivePopGestureRecognizer.enabled = NO; doesn't work and possibly a bug for iOS 8 – gabbler Sep 29 '14 at 12:32
  • Were you able to solve the bug in ios 8? i checked its not working for me. – Dhara Sep 29 '14 at 12:47
  • Using the posted code I can successfully disable the interactivePopGesture, while using self.navigationController.interactivePopGestureRecognizer.enabled = NO would not disable it. – gabbler Sep 29 '14 at 12:54
  • If this doesn't work and you're english then make sure you're not using the english spelling. ;) – olive Oct 12 '14 at 19:39
  • @olive,what do you mean by that? – gabbler Oct 13 '14 at 00:15
4

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;

    }

}
Userich
  • 316
  • 4
  • 4
2

Following solution!!!

- (void)viewWillAppear:(BOOL)animated {
      [super viewWillAppear:animated];

      __weak id weakSelf = self;
      self.navigationController.interactivePopGestureRecognizer.delegate = weakSelf;  }

The navigation controller installs this gesture recognizer on its view and uses it to pop the topmost view controller off the navigation stack. You can use this property to retrieve the gesture recognizer and tie it to the behavior of other gesture recognizers in your user interface. When tying your gesture recognizers together, make sure they recognize their gestures simultaneously to ensure that your gesture recognizers are given a chance to handle the event.

Usually we need to implement when you manually add a Navigation Bar or when we customized Navigation Controller

Swift Version

Add on RootViewController the protocol UIGestureRecognizerDelegate and viewWillAppear add:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.interactivePopGestureRecognizer!.delegate =  self }
Community
  • 1
  • 1
Bruno
  • 1,592
  • 13
  • 14
  • This works following the solution posted above with iOS 8 but without the compiler warnings so I'm not sure why the down vote from others, please comment why. – David Douglas Feb 06 '15 at 19:19
  • Does not appear to be a complete solution. If it is, then explain why it works. If it is not, then mention which answer you are modifying. – ToolmakerSteve Sep 07 '16 at 12:33
  • how to write this in swift got the **`Cannot assign value of type (VC) to type 'UIGestureRecognizerDelegate?`** – Dan Sep 28 '16 at 14:19
-6
- (void)viewDidLoad {
    [super viewDidLoad];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    });

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    });
}

I think the easy way is above. You should not make gesture enable directly in viewDidLoad()。 But you can change it's state after viewDidAppeared(). You know it's After, :-D

xu tong
  • 453
  • 4
  • 8
  • 1
    Don't do this. First, it isn't clear how this would solve the problem. It disables, then a while later enables. What are you trying to do? Second, it is poor programming practice to use delays. There is no guarantee that a given delay is long enough, and yet not too long - may either happen before the recognizer is installed, or happen late enough that the user could manage to start a swipe before the code runs. Don't use delays; instead use the methods that Apple provides, `viewWillAppear` or `viewDidAppear`, *as seen in other answers posted long before your answer*. – ToolmakerSteve Sep 07 '16 at 12:36