InteractivePopGestureRecognizer causing app freezing

12.9k views Asked by At

In my app I have different controllers. When I push controller1 to navigation controller and swipe to back, all works good. But, if I push navigation controller1, and into controller1 push controller2 and try to swipe to back I get a frozen application. If go back through back button all works fine.

How can I catch the problem?

9

There are 9 answers

3
glyuck On BEST ANSWER

I had similar problem with freezing interface when using swipe-to-pop gesture. In my case the problem was in controller1.viewDidAppear I was disabling swipe gesture: self.navigationController.interactivePopGestureRecognizer.enabled = NO. So when user started to swipe back from contorller2, controller1.viewDidAppear was triggered and gesture was disabled, right during it's work.

I solved this by setting self.navigationController.interactivePopGestureRecognizer.delegate = self in controller1 and implementing gestureRecognizerShouldBegin:, instead of disabling gesture recognizer:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)] &&
            gestureRecognizer == self.navigationController.interactivePopGestureRecognizer) {
        return NO;
    }
    return YES;
}
0
enqingchen On

I solved my problem by UINavigationController interactivePopGestureRecognizer working abnormal in iOS7 and set self.navigationController.interactivePopGestureRecognizer.delegate = self; on every viewcontroller's - (void)viewWillAppear:(BOOL)animated

0
Sakule On

I had problem when swipe back on first controller and then tap on tableViewCell, I think that force touch swipe from edge is registering swipe back before switches app, sometimes UI get stuck, sometimes when I swipe back it starts entering on destination controller. I solved problem with this extension, it worked for me and it is simple solution. Swift 4.2

extension UINavigationController:UINavigationControllerDelegate {

    open override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
    }

    public func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        if responds(to: #selector(getter: self.interactivePopGestureRecognizer)) {
            if viewControllers.count > 1 {
                interactivePopGestureRecognizer?.isEnabled = true
            } else {
                interactivePopGestureRecognizer?.isEnabled = false
            }
        }
    }
}
1
Edward On

My solution is exchange self.navigationController.interactivePopGestureRecognizer.delegate between selfImplementDelegate and SystemDelegate

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_tableView reloadData];
    _oldReturnDelegate = self.navigationController.interactivePopGestureRecognizer.delegate;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.interactivePopGestureRecognizer.delegate = _oldReturnDelegate;
    [super viewWillDisappear:animated];
}
0
Vladislav Kliutko On

Swift 4

Add this code to root navigation controller

func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    return self == self.navigationController?.topViewController ? false : true
}

Add UIGestureRecognizerDelegate protocol

self.navigationController?.interactivePopGestureRecognizer?.delegate = self
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
2
cnu On

Swift 4:

Set the delegate,

self.navigationController?.interactivePopGestureRecognizer?.delegate = self

Implement the delegate method,

extension YourVC: UIGestureRecognizerDelegate{
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer == self.navigationController?.interactivePopGestureRecognizer && conditionToDisableTheGesture {
            return false
        }else{
            return true
        }
    }

}
0
sinh99 On

I had same issue and I found below solution. add below controller

#import <UIKit/UIKit.h>
@interface CBNavigationController : UINavigationController     <UIGestureRecognizerDelegate,UINavigationControllerDelegate>
@end

#import "CBNavigationController.h"
@interface CBNavigationController ()
@end
@implementation CBNavigationController
- (void)viewDidLoad
{
NSLog(@"%s",__FUNCTION__);
__weak CBNavigationController *weakSelf = self;

if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
{
    self.interactivePopGestureRecognizer.delegate = weakSelf;
    self.delegate = weakSelf;
}
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
NSLog(@"%s",__FUNCTION__);

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

[super pushViewController:viewController animated:animated];
}

#pragma mark UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController
   didShowViewController:(UIViewController *)viewController
                animated:(BOOL)animate
{
NSLog(@"%s",__FUNCTION__);

// Enable the gesture again once the new controller is shown

if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
    self.interactivePopGestureRecognizer.enabled = YES;
}
@end

Can refer below link

http://keighl.com/post/ios7-interactive-pop-gesture-custom-back-button/

3
Ryan On

My solution was to add a delegate to the navigation controller. Then disable the pop gesture recogniser in the root view controller only. YMMV.

#pragma mark - UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    BOOL isRootVC = viewController == navigationController.viewControllers.firstObject;
    navigationController.interactivePopGestureRecognizer.enabled = !isRootVC;
}
0
Kaey On

I would suggest you to try this. This works perfectly for me. You can still enjoy Interactive swipe.

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)] &&
      gestureRecognizer == self.navigationController.interactivePopGestureRecognizer) {
    if(self.navigationController.viewControllers.count<=1)
    {
      return NO;
    }
  }
  return YES;
}