开发者

UIPopoverController presentPopoverFromRect problem

开发者 https://www.devze.com 2023-04-02 12:43 出处:网络
I am tryin开发者_运维问答g to present a UIPopoverController when a UIButton is clicked. Here\'s my code:

I am tryin开发者_运维问答g 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:

UIPopoverController presentPopoverFromRect problem

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.

UIPopoverController presentPopoverFromRect problem

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:

UIPopoverController presentPopoverFromRect problem


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];


And Swift, Swift, Swift of course!

pc.presentPopoverFromRect(sender.bounds, inView: sender, 
permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
0

精彩评论

暂无评论...
验证码 换一张
取 消