iPad Click on the transparent area to cancel the pop-up window

Reprinted from: http://stackoverflow.com/questions/2623417/iphone-sdk-dismissing-modal-viewcontrollers-on-ipad-by-clicking-outside- of-it/12454720#12454720

Copy paste this code in your ModalViewController:-(void ) viewDidAppear:(BOOL)animated {[super viewDidAppear:animated]; //Code for dissmissing this viewController by clicking outside it UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)]; [recognizer setNumberOfTapsRequired:1]; recognizer.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view [self.view.window addGestureRecognizer:recognizer];}-(void)handleTapBehind:(UITapGestureRecognizer *)sender {if (sender.state == UIGestureRecognizerStateEnded) {CGPoint location = [sender locationInView:nil]; //Passing nil gives us coordinates in the window //Then we convert the tap's location into the local view's coordinate system, and test to see if it's in or outside. If outside, dismiss the view. if (![self.view pointInside :[self.view convertPoint:location fromView:self.view.window] withEvent:nil]) {// Remove the recognizer first so it's view.window is valid. [self.view.window removeGestureRecognizer:sender]; [self dismissModalViewControllerAnimated :YES];}} }

Leave a Comment

Your email address will not be published.