[英]UIPopoverController: Why my popover doesn't appears where i want to?

Simple: A view, i present a UIPopoverController in a CGRect using presentPopoverFromRect... and neither the arrow or the popover frame appear even near to the coordinates i asked for in the rect i passed into. Any clues? I've been trying to figure out this by myself but am giving up. Here is the code:


    myContentController = [[MyContentController alloc] initWithNibName:myNibName bundle:[NSBundle mainBundle]];
    // This works pretty well. actually when i show the popover
    // i see everything inside as it's supposed to.
   popover = [[UIPopoverController alloc] initWithContentViewController:myContentController];
    [popover setContentController:myContentController];
popover.delegate = self;
CGPoint touchPointInView = [self touchPoint];//This is working fine too.I've been checking with NSLog.
popover.ContentSize = myPopoverSize;//In this case {320,480}
 [popover presentPopoverFromRect:CGRectMake(touchPoint.x,touchPoint.y,myPopoverSize.width,myPopverSize.height)

What happens next? the popover doesn't shows where it should be. If i pass {0,0} it shows in the middle of the screen as if the view size were (768,512). I checked all the view dimensions and they are all ok, frame,bounds, etc... . Does anyone knows what am i doing wrong?


2 个解决方案



The CGRect you give to presentPopoverFromRect is the rect that it will display the popover next to (not in). Depending on the location of the rect, the popover will appear on an appropriate side of the specified rect.


If you want the popover to appear at a specific point, give it a rect with the origin as the point and the size as 1,1. So the CGRectMake in the presentPopoverFromRect line should be:

如果你想要彈出窗口出現在一個特定的點上,給它一個矩形,原點為點,尺寸為1 1。所以在presentPopoverFromRect線上的CGRectMake應該是:


Also make sure that the touchPoint is relative to the inView (self.view in your case).

還要確保接觸點相對於inView (self)。查看你的情況)。

By the way, there are a few other errors in the code (probably just typos in the question):


  • setContentController should be setContentViewController
  • setContentController應該setContentViewController
  • popover.ContentSize should be popover.popoverContentSize
  • 彈出窗口。ContentSize應該popover.popoverContentSize
  • myPopverSize.height should be myPopoverSize.height (but this will be replaced by 1)
  • myPopverSize。高度應該myPopoverSize。高度(但這個將被1取代)



In my case the reason of this problem was following. It is also possible, that your view is a tableView. So, when you scroll it and try to show UIPopover in a rect in that tableView, it may not be shown. I used this:


[_popoverController presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];



粤ICP备14056181号  © 2014-2020 ITdaan.com