0xSina 0xSina - 1 month ago 25
iOS Question

UIPopoverController presentPopoverFromRect problem

I am trying to present a UIPopoverController when a UIButton is clicked. Here's my code:

- (IBAction)showColumnChooser:(id)sender {

ColumnChooserTVC *vc = [[ColumnChooserTVC alloc] init];

[vc setSelections:allColumns];
[vc setDelegate:self];
UIPopoverController *pc = [[UIPopoverController alloc] initWithContentViewController:vc];


[pc presentPopoverFromRect:[sender frame] inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
[vc release];
}


With the arrow direction as "Any", it completely obscures the button, here's what it looks like:
enter image description here

If I make the direction "Right", it's a little better but still there's some room between the popover and the button and it doesn't seem right.
enter image description here
I don't want to do some "tricks" or "hacks" and use a CGRect on a trial/error basis, I want to know what's the proper way of doing this? Thanks.

Here's the button in interface builder as requested by Neckto:
enter image description here

Answer

I think you are mixing coordinate systems. At each level in your view-hiearchy, the origin is moved. So the location of [sender frame] in self.view is not where the button is located.

Try:

[pc presentPopoverFromRect:[sender bounds]
                    inView:sender
  permittedArrowDirections:UIPopoverArrowDirectionAny 
                  animated:YES];