Search code examples
iosobjective-cuipopovercontroller

How to prevent UIPopoverPresentationController from being dismissed when clicking outside popover?


In my universal iOS 8 app, I am presenting a popover using using UIPopoverPresentationController as seen below from prepareForSegue:

FavoriteNameViewController *nameVC = segue.destinationViewController;
UIPopoverPresentationController *popPC = nameVC.popoverPresentationController;
popPC.delegate = self;

And with this delegate method.

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone;
}

In this particular case, I'm presenting a view controller that looks like an alert, but isn't.

enter image description here

Now my issue is that the user can click outside of this popover and it gets dismissed. There's no real problem with that except that's not how alerts work and I would like this to emulate an alert.

I see that UIPopoverControllerDelegate had a method called popoverControllerShouldDismissPopover:, but UIPopoverPresentationControllerDelegate doesn't have that method, and I believe I need to use the latter.


Solution

  • You need to set the popover controller's passthroughViews to nil and the view controller's modalInPopover to YES.