Tuesday, May 19, 2015

How to Disable Default Popping Topmost UIViewController by Swipe from Left Edge


From iOS7, user could pop to parent UIViewController by swiping from screen’s left edge to some points in the middle. Also, UIScreenEdgePanGestureRecognizer is introduced since then, which could help develper add more ways to interact with the user.

Whlie, a question raised recently, how can I disable it?

The UIViewControllers are “Push” or “Pop” from the ViewController Stack, sometimes we call it UINavigationController. This is the entry where developer could use to access all the UIViewController in the Stack. Also, each UIViewController been pushed into the Stack will have an easy access to this UINavigationController.

Back to our question, how can we disable it?

After reading the iOS Developer Doc of UINavigationController, I found interactivePopGestureRecognizer Property, which is 

The gesture recognizer responsible for popping the top view controller off the navigation stack. (read-only)

then, if we can disable this property, we could disable the user popping to parent UIViewController by swiping.

Here is the Code:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return NO;
}

Also, Add UIGestureRecognizerDelegate to the UIViewController.

No comments :

Post a Comment