Christopher Jones
Christopher Jones

Reputation: 47

Dismiss Modal view controller with tap AND/OR edge swipe

I'm working on an iOS app with lots of gestures integrated for navigation. When certain items are tapped, a modal view controller slides up with options to select inside, and a 'done' button across the top of the view to dismiss said modal VC.

The 'done' button doesn't quite 'feel' right as there are so many other gesture-based controls throughout the app, and I was wondering if: anybody out there know of an (easy) way I could integrate a 'swipe from the top edge of the screen' to dismiss this modal?

I don't want to get rid of the 'done' button, though. Hope that makes sense! Currently, my modalVC's *.m file's got:

-(IBAction)doneButtonPressed:(id)sender
{[self dismissViewControllerAnimated:YES completion:nil]; } 

in there… Pretty straightforward. Tapping the 'done' button totally works, too. No problems at this point

Upvotes: 2

Views: 3758

Answers (1)

Suhas
Suhas

Reputation: 1500

You can use a UISwipeGestureRecognizer to achieve this.

UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] 
    initWithTarget:self action:@selector(userSwiped:)];

swipeRecognizer.direction = UISwipeGestureRecognizerDirectionDown;

[modalController.view addGestureRecognizer:swipeRecognizer];

//Action method
- (void)userSwiped:(UIGestureRecognizer *)sender
{
   [self dismissViewControllerAnimated:YES completion:nil]; 
}

iOS uses swipe from top corner to display the Notification centre. It's therefore not recommended to relate this gesture to any action in your app.

The above sample recognises a top to bottom swipe anywhere on the modal view and dismisses it.

Upvotes: 7

Related Questions