Search code examples
iosmodalviewcontrollerpresentmodalviewcontrollertouch-event

Presenting view controller response touch events when modal view controller is presented


Here is the code:

UIStoryboard *myStoryboard = [UIStoryboard storyboardWithName:@"StoryboardName" bundle:nil];
UIViewController *presentedViewController = [myStoryboard instantiateViewControllerWithIdentifier:@"IdentifierName"];
presentedViewController.modalPresentationStyle = UIModalPresentationFormSheet;

presentingViewController presentViewController:presentedViewController animated:NO completion:nil];

the view of presentingViewController would not response any touch event, but I want it to take touch events,what should I do? Appreciate for any suggestion!


Solution

  • This is the expected behavior and should be what you see in most apps. Apple's App Store app does allow you to tap outside the modal form sheet to dismiss it, however, so there's some precedent for it being acceptable, in a limited way. If your design absolutely requires it, consider setting up and tearing down a window-based gesture recognizer in the presented view controller.

    For example, to implement the tap-outside-to-dismiss behavior, configure a tap gesture recognizer in viewDidAppear:. You can remove it on viewWillDisappear: if it still exists at the time that is called.

    In the tap handler, you'd want to get the location of the tap in window coordinates, ie:

    CGPoint tapPoint = [recognizer locationInView:nil];
    

    You could then do some work with geometry to check and see if the tap occurred on the modally-presented view or outside (over the dimmed region that corresponds to your presenting view controller). UIView has a pointInside:withEvent: method that might help with that.