Adjust UIPopoverController position after resize

12,107

Solution 1

I think this may be the wrong way to go about it, since you're having to re-do the built-in behaviour that positions the arrow to begin with.

I don't resize popover content in viewDidAppear. I set the contentSizeForViewInPopover property in the view controller's viewDidLoad method, e.g.:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.contentSizeForViewInPopover = CGSizeMake(320, 155); // sized for a 3-row UITableView
}

(Quick warning: if you're developing a universal app, this code will cause a run-time crash on devices running 3.1.x and below.)

You can also set the content size for the popover controller before you present it, which should take care of your problem. Check out the popoverContentSize property.

Solution 2

According to this answer, you can call presentPopoverFromRect:inView: on the popover again and it will reposition the arrow. I haven't tested this myself.

Share:
12,107
titaniumdecoy
Author by

titaniumdecoy

Updated on June 04, 2022

Comments

  • titaniumdecoy
    titaniumdecoy about 2 years

    I have a UIPopoverController containing a UITableView. The popover is resized in its view controller's -viewDidAppear function to fit the contents of the table. While the popover resizes properly, its arrow is usually no longer pointing at the original CGRect. Is there a way to force the popover to reposition itself after a resize so that its arrow is pointing at its intended target?

    EDIT: I can't set the size of the popover in -viewDidLoad since the table view does not load its data until -viewDidAppear is called, and as a result I do not know what size the popover should be until then. In addition, I resize the popover when one of the table view cells is clicked to display another view and this also results in the arrow no longer pointing at its intended target.

  • titaniumdecoy
    titaniumdecoy almost 14 years
    Thanks, but that won't work in my situation; see my updated question.
  • Stephan Burlot
    Stephan Burlot over 13 years
    This is documented in Technical Q&A QA1694: developer.apple.com/iphone/library/qa/qa2010/qa1694.html
  • Kshitiz Ghimire
    Kshitiz Ghimire about 13 years
    @ shaggy frog popovercontentsize is good idea self.popoverController.popoverContentSize = CGSizeMake(320, 300); will do da job. thanks man
  • David James
    David James over 10 years
    Note contentSizeForViewInPopover is deprecated as of 7.0