3
votes

My question is similar to this <question> , in which the OP's objective was to show an array of UIBarButtonItems as navigationItem.rightBarButtonItems without the barbuttons' outlines/borders. His solution, initializing the UIBarButtonItem with UIButton custom view, gives the desired appearance, and the UIButton target/action pattern works fine for calling a method, but does not work for positioning a UIPopoverController.

What I'm trying to do is present an actionSheet in a popover (i.e present an actionSheet on iPad), and (from a different button) present a print controller popover, with the popover arrow pointing to the selected bar button, as the rightBarButton items do in the iPad Mail app.

The problem is, the methods for presenting the actionSheet or a popover:

[UIActionSheet showFromBarButtonItem:bbtn animated:YES];

or

[UIPrintInteractionController presentFromBarButtonItem:printButton animated:YES completionHandler:completionHandler]

don't work when called with a UIButton sender. Also, the methods for presenting from a CGRect, e.g.

[controller presentFromRect:rect inView:self.view animated:YES completionHandler:completionHandler]

don't position the popover pointer correctly.

I have tried converting the button frame using UIView convertRect:toView: and presenting the popover from the converted rect, but I'm either doing it wrong or it is not able to convert correctly from the UIBarButtonItem superview which has no frame property. The popover is presented but the popover arrow does not point to the selected button.

2

2 Answers

2
votes

Try to compute converted button frame in your navigation controller's view coordinates. E.g.

CGRect convertedRect = [self.navigationController.view convertRect:button.bounds fromView:button];
[controller presentFromRect:convertedRect inView:self.navigationController.view animated:YES completionHandler:completionHandler];
0
votes

I had the exact same problem as you. One solution that I found is to use a completely transparent image for the background of the UIBarButton item.

UIBarButtonItem *button = [[BWBarButton alloc] initWithImage:[UIImage imageNamed:imageName] style:UIBarButtonItemStylePlain target:target action:selector];

[button setBackgroundImage:[UIImage imageNamed:@"barimage"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

Here barimage is a 60x60 transparent image. I think you can use a 1x1px image.

Good luck!