Search code examples
uinavigationcontrolleruiswipegesturerecognizerbackbarbuttonitem

Swipe gesture instead of backbarbuttonitem with UINavigationController


I've looked around and I can't for the life of me find a way to replace the backbarbuttonitem with a swipe gesture. I want to swipe left and have the app revert back to the previous view controller. Just to clarify I'm using a UINavigationController to manage all my View Controllers. Any ideas? Or is this not possible? Thanks!


Solution

  • I worked it out and now I feel stupid for asking this question! Here is the code I used:

    I created a Swipe Gesture in the ViewDidLoad function:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
    
    // -- EDIT Added the allocation of a UIGestureRecognizer -- //
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(gotoPreviousView:)];
        swipeRight.numberOfTouchesRequired = 1;
        swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
        [self.view addGestureRecognizer:swipeRight];
    }
    

    Then I simply created this...

    - (void)gotoPreviousView:(id)sender {
        [self.navigationController popViewControllerAnimated:YES];
    }
    

    Easy! Looked too far into it and the answer was staring me in the face. Hopefully if you don't know how, you'll see this and not make the same rookie error I did ha...