Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting Swipe In UINavigationBar

I am trying to get my view controller to detect swipes in the UINavigationBar that is automatically displayed by my app, but it refuses to detect swipes. Is there any way I can do it?

like image 659
Matt Avatar asked May 10 '26 17:05

Matt


1 Answers

Supposing you want to detect swipes to the left in your navigation bar, you could do something like this when you create the navigation controller:

   UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewSwipedLeft:)];
   [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
   [self.navigationController.navigationBar addGestureRecognizer:swipeLeft];

and then create a method like the one below to handle it:

-(void) didSwipedLeft: (UISwipeGestureRecognizer *) gesture {

  if (gesture.state != UIGestureRecognizerStateEnded) {
      return;
  }

  //do something    
}

OBS: As you navigation controller is a class that will remain alive for several steps of you application life cycle, it is important to pay attention to that and add the gesture recognizer only when you create the navigation controller (which means only add it once) so that you dont keep piling gestures recognizer one over each other, wich will lead not only to a memory leak, but also might make your method didSwipedLeft to be called more than once.

like image 101
Felipe Sabino Avatar answered May 12 '26 07:05

Felipe Sabino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!