简体   繁体   中英

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!

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.

Here is the simple way to present viewcontroller...

  ViewController *view = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
  [self presentViewController:view animated:YES completion:nil];

Enjoy code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM