15

after a bit of searching, I couldn't find an answer to something that seems like it would be useful to many.

Is there a way to make a UIPopoverController not dismiss when the user clicks somewhere on the outside? I want the user to have to use a cancel button (Yes, i realize this probably violates Apple's HIG somehow, but it's a rare case and makes sense from a User experience perspective).

Thanks for any help.

Jesse Naugher
  • 9,780
  • 1
  • 41
  • 56
  • 2
    "I realize this ***probably*** violates Apple's HIG…" did you read it? –  Jun 13 '11 at 15:54
  • I've read it quite a few times, but it also makes perfect sense for the design (opening a file from another app to save), and also seen quite a few apps do it. :/ – Jesse Naugher Jun 13 '11 at 15:56

3 Answers3

26

Just set the modalInPopover property on the UIViewController being displayed in the UIPopoverController.

popover = [[UIPopoverController alloc] initWithContentViewController:content];
content.modalInPopover = YES;
[popover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

Be aware that, as of iOS5, you have to set modalInPopover inside of -viewDidAppear.

Community
  • 1
  • 1
jlstrecker
  • 4,953
  • 3
  • 46
  • 60
21

You can do hit-tests on where the tap occurred and in your popover's delegate return NO. - (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController

fearmint
  • 5,276
  • 2
  • 33
  • 45
  • 1
    +1: this, this is perfect. must have skimmed over it in the documentation. Thanks. It also doesn't get called when you call dismissPopoverAnimated: so don't even have to hit test really. Thanks – Jesse Naugher Jun 13 '11 at 15:58
  • 1
    NB: This delegate method (indeed all methods of `UIPopoverControllerDelegate`) has been deprecated: https://developer.apple.com/documentation/uikit/uipopovercontrollerdelegate/1624661-popovercontrollershoulddismisspo?language=objc. – adamjansch Mar 09 '18 at 14:53
1

So, I realize this is an old question. However, there's an easier answer for anyone searching for a solution today.

If you use a Storyboard Segue, you can set the passthrough property on the segue allowing interaction with other objects in the view. If you do so, clicking outside of the bounds of the popover won't close the popover.

Here's some more info: What are Anchor and Passthrough used for in popover segues?

And here's a excerpt from the Apple documentation:

To allow the user to interact with the specified views and not dismiss the popover, you can assign one or more views to the passthroughViews property.

Community
  • 1
  • 1
SteveSTL
  • 998
  • 3
  • 13
  • 21