2

When I present a ViewController in an popup, it resizes the view. To avoid that, I have to set the view size manually using UIPopoverController setPopoverContentSize:settingsViewSize

I have tried to override contentSizeForViewInPopover

-(CGSize)contentSizeForViewInPopover
{
    return self.view.frame.size;
}

...but to no avail.

Suggestions?

  • Possible duplicate: http://stackoverflow.com/questions/2926308/uipopovercontroller-w-uinavigationcontroller-subview-contentsizeforviewinpopove – Martin R Sep 08 '12 at 08:20
  • 1
    Martin, it does not seem to be the same issue. – Anders Sewerin Johansen Sep 08 '12 at 08:22
  • Try to set an explicit size in `contentSizeForViewInPopover`: `return CGSizeMake(200., 200.)`. – Martin R Sep 08 '12 at 08:27
  • 1
    Martin, that's what I want to avoid. I want to use IB to design my views, and not have to update code if I change the size of them. This is a huge issue, as my app is multi-language, and some views have different size depending on the language. – Anders Sewerin Johansen Sep 08 '12 at 11:03
  • OK, I understand. But perhaps it helps to narrow down the problem: Does setting an explicit size work? – Martin R Sep 08 '12 at 11:09
  • Setting an explicit size in the viewController contentSizeForViewInPopover works, but has the same issues as the current solution - another place to change for each language. Setting a size using return self.view.frame.size; as shown in the question does not. – Anders Sewerin Johansen Sep 08 '12 at 11:47
  • Your `contentSizeForViewInPopover` method worked in my test app. Do you have a navigation controller inside the popover? – Martin R Sep 08 '12 at 20:57
  • Nope, a quite vanilla view controller. – Anders Sewerin Johansen Sep 08 '12 at 21:16
  • If you set a breakpoint in `contentSizeForViewInPopover`, what is the value of `self.view.frame` and `self.view.bounds` ? – Martin R Sep 09 '12 at 01:29
  • Both are 768 x 1004, so size of the entire screen. Not what I want. – Anders Sewerin Johansen Sep 09 '12 at 05:48
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16461/discussion-between-martin-r-and-anders-sewerin-johansen) – Martin R Sep 09 '12 at 07:56
  • Thank you for the offer. Sorry, but kinda hard to make work for me. I am on Central European Time, and work most of the day in a Day Job where I'm not allowed web access to unapproved sites. – Anders Sewerin Johansen Sep 10 '12 at 04:40

1 Answers1

1

Your code doesn't work because contentSizeForViewInPopover is called before viewWillDisplay, so the frames of the views haven't been set yet. There are a few things you can do though.

If you are using a UITableViewController a little known trick to sizing your UIPopoverViewController to match the height of your UITableView is to use the tableView's rectForSection method to give you the height. Use the height in your viewController's contentSizeForViewInPopover like this:

- (CGSize)contentSizeForViewInPopover 
{
    // Currently no way to obtain the width dynamically before viewWillAppear.
    CGFloat width = 200.0; 
    NSInteger lastSectionIndex = [self.tableView numberOfSections] - 1;
    CGRect rect = [self.tableView rectForSection:lastSectionIndex];
    CGFloat height = CGRectGetMaxY(rect);
    return (CGSize){width, height};
}

Or alternately, you can manually set the contentSizeForViewInPopopver property in your viewController's viewWillAppear: method and set the popover's size when you create it.

So, in your viewController which will be in the popover, you'll have this:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.contentSizeForViewInPopover = self.view.size
}

And you'd create your popover like this:

MyViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"vc"];
self.currentPopover = [[UIPopoverController alloc] initWithContentViewController:vc];
[self.currentPopover presentPopoverFromRect:sender.bounds 
                                     inView:sender 
                   permittedArrowDirections:UIPopoverArrowDirectionUp 
                                   animated:YES];

// resize the popover to match what you set in vc viewDidLoad
[self.currentPopover setPopoverContentSize:vc.contentSizeForViewInPopover animated:NO];
memmons
  • 40,222
  • 21
  • 149
  • 183
  • contentSizeForViewInPopover is deprecated in iOS7. Try this is **viewDidAppear**: `self.preferredContentSize = self.tableView.contentSize;` – Sam Brodkin Mar 07 '14 at 22:11