Cole
Cole

Reputation: 726

dismiss UIPopoverController (if visible) after tapping a UIBarButtonItem

I'm trying to dismiss any currently visible UIPopoverControllers if/when another UIBarButtonItem is tapped as seen on Pages for the iPad when tapping between Styles/Media/Tools etc.

I've done this, but it's too specific. Looking for something generic.

if ([popoverController isPopoverVisisble]) {
[popoverController dismissPopoverAnimated:YES];
}

Thanks!

Upvotes: 1

Views: 2801

Answers (3)

vaddieg
vaddieg

Reputation: 626

[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
popoverController.passthroughViews = @[];

Works for me

Upvotes: 0

Solo
Solo

Reputation: 158

randallmeadows answer will NOT work. UIBarButtonItem is not descendant of UIView, which means you cannot add it to passthroughViews.

The only solution I found for now is to create UIBarButtonItem with custom UIButton using

   UIBarButtonItem *b =  [[UIBarButtonItem alloc] initWithCustomView:button]

and then

 popoverController.passthroughViews = [NSArray arrayWithObject:b.customView];

But be prepared that you'll loose all the styling - you cannot create UIButton that looks like UIBarButtoItem too easily.

Upvotes: 1

randallmeadows
randallmeadows

Reputation: 863

Did you set the passthroughViews property of the popover controller? If you do this, then taps outside the popover will not cause the popover to automatically dismiss, but will instead be sent to the views in that array. You should be able to add the UIBarButtonItem to this array, and then dismiss the popover in that handler.

Upvotes: 2

Related Questions