I am using a UIPresentationController
to show a custom modal. the presentation controller has a UIView
dimming view animated in an out when it is being shown. The modal itself is a UIViewController
added to the presentation controller's container.
I can only call [self dismissViewControllerAnimated:NO completion:nil]
from the embedded UIViewController. But I cannot do the same from the UIPresentationController
. But that's where the dimming view is.
I'd like to avoid adding additional invisible views to the modal or use NSNotificationCenter
if possible.
How do you dismiss a UIPresentationController
by tapping its dimming view? Does it make sense? Is it possible?
Okay, I found it. You can reach the shown UIViewController to dismiss via self.presentedViewController
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
You can try this :
- (void)viewDidAppear:(BOOL)animated {
if (!self.tapOutsideRecognizer) {
UITapGestureRecognizer *tapOutsideRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)];
self.tapOutsideRecognizer.numberOfTapsRequired = 1;
self.tapOutsideRecognizer.cancelsTouchesInView = NO;
self.tapOutsideRecognizer.delegate = self;
[self.view.window addGestureRecognizer:self.tapOutsideRecognizer];
}
}
- (void)handleTapBehind:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
CGPoint location = [sender locationInView:nil];
if (![self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil]) {
[self.view.window removeGestureRecognizer:sender];
[self back:sender];
}
}
}
- (IBAction)back:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With