is there a way NOT to have the popover dismissed when pressing outside it?

萝らか妹 提交于 2019-11-30 05:30:21

You need to set the passthroughViews property. From the documentation:

An array of views that the user can interact with while the popover is visible.

@property (nonatomic, copy) NSArray *passthroughViews

When a popover is active, interactions with other views are normally disabled until the popover is dismissed. Assigning an array of views to this property allows taps outside of the popover to be handled by the corresponding views.

Set passthroughViews to an array of view(s) that you want to handle the touch event instead of just dismissing the popover.

The accepted answer does not really answer the question, "is there a way NOT to have the popover dismissed when pressing outside it?", imo. It does give a possible view but could require hackish access to all parent views and determining what views are on the screen etc. The question could be rephrased as, "how do I make a popover view modal?"

You would do this like so, with a done button to close the popover:

UIViewController* vc = [[[UIViewController alloc] init] autorelease];

UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done"] style:UIBarButtonItemStyleDone target:self action:@selector(processDoneAction)] autorelease];

[vc.navigationItem setLeftBarButtonItem:doneButton];

vc.modalInPopover = YES;
//If you want full screen:
vc.modalPresentationStyle = UIModalPresentationFullScreen;
vc.wantsFullScreenLayout = YES;

UINavigationController* navC = [[[UINavigationController alloc] initWithRootViewController:vc] autorelease];

UIView* view = create your view

vc.view = view;

UIPopoverController* pc = [[[UIPopoverController alloc] initWithContentViewController:navC] autorelease];
pc.delegate = self;
self.popoverController = pc;

Then you'll in your processDoneAction method you will need to dismiss the popover. Other considerations would be dismissing and redisplaying on device orientation changes, but I will leave that to another exercise as that has been answered previously on stackoverflow.

There is a very simple and legit solution. In the view controller that presents your UIPopoverController, conform to the UIPopoverControllerDelegate protocol and implement the following delegate method. I just tested this and it does prevent popover to dismiss.

- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
{
    return NO;
}

Just make sure that you have set the delegate of your popover controller to the view controller that implements this.

You can dismiss the popover by using [popoverController dismissPopoverAnimated:NO]; method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!