12

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

0xSina
  • 20,973
  • 34
  • 136
  • 253
  • Sorry, but I don't understand what you are asking for. Can you give more details, please. You want popover arrow to point to another place? – Nekto Sep 04 '11 at 10:17
  • Well, I want the arrow to point directly to the button. In this case, do you see the "bars/lines" icon? That what I want the popover arrow to point to. In first example, it completely obscures the icon/button, in second example, it's a bit better but it doesn't point to the button, but a little up and to the left. Hope it's cleat now. thanks. – 0xSina Sep 04 '11 at 10:35
  • Then the problem is in `[sender frame]`. It is too large, probably. How do you create that button? – Nekto Sep 04 '11 at 10:49
  • It's from interface builder. I simply hooked it up to the IBAction – 0xSina Sep 04 '11 at 10:56
  • Select that button in IB, make screenshot and show it to us. I want to see the frame of this button – Nekto Sep 04 '11 at 11:06
  • I have updated my question with screenshot of the button. – 0xSina Sep 04 '11 at 11:24

2 Answers2

50

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];
Mats
  • 8,528
  • 1
  • 29
  • 35
0

And Swift, Swift, Swift of course!

pc.presentPopoverFromRect(sender.bounds, inView: sender, 
permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
vkalit
  • 647
  • 8
  • 19