14

In the new iOS 7 UINavigationController, there is a swipe gesture to switch between views. Is there a way to detect or intercept the gesture?

lhan
  • 4,585
  • 11
  • 60
  • 105
Steven
  • 1,088
  • 1
  • 8
  • 16

2 Answers2

34

The interactive pop gesture recognizer is exposed through UINavigationController's interactivePopGestureRecognizer property. You can add your own controller as a target of the gesture recognizer and respond appropriately:

@implementation MyViewController

...

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.navigationController.interactivePopGestureRecognizer addTarget:self 
                                                                  action:@selector(handlePopGesture:)];
}


- (void)handlePopGesture:(UIGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        // respond to beginning of pop gesture
    }
    // handle other gesture states, if desired
}

...

@end
Austin
  • 5,625
  • 1
  • 29
  • 43
14

Here is Austin's answer, in Swift. Using this post to construct the selector, I found the following to work.

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.interactivePopGestureRecognizer?.addTarget(self, action:#selector(self.handlePopGesture))
}

func handlePopGesture(gesture: UIGestureRecognizer) -> Void {
    if gesture.state == UIGestureRecognizerState.Began {
        // respond to beginning of pop gesture
    }
}
Community
  • 1
  • 1
mario314
  • 173
  • 1
  • 4