Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss a UIPresentationController by tapping its dimming view

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.

The problem

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?

like image 529
Bernd Avatar asked Feb 08 '23 06:02

Bernd


2 Answers

Okay, I found it. You can reach the shown UIViewController to dismiss via self.presentedViewController

[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
like image 182
Bernd Avatar answered Feb 10 '23 22:02

Bernd


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];
}
like image 42
Pipiks Avatar answered Feb 10 '23 22:02

Pipiks