4

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?

Bernd
  • 11,133
  • 11
  • 65
  • 98

3 Answers3

6

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

[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
Bernd
  • 11,133
  • 11
  • 65
  • 98
  • where did you put this? it's not working for me. I'm getting the error unrecognized selector sent to instance – Ishmeet Jul 18 '16 at 16:45
  • Got it to work. Apparently I wasn't setting the target for gesture recognizer properly. Thanks for the help. – Ishmeet Jul 18 '16 at 16:48
2

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];
}
Pipiks
  • 2,018
  • 12
  • 27
  • That's the problem. There is no `dismissViewControllerAnimated` in the `UIPresentationController` – Bernd Dec 16 '15 at 18:25
0

Yahh it is possible.. first you have to add Tap gesture on dimming view and add in Tap gesture action...

[self dismissViewControllerAnimated:YES completion:nil];

this will solve your problem.

Deep
  • 416
  • 6
  • 15