How To Dynamically change the contentSize of UIPopoverController? How To Dynamically change the contentSize of UIPopoverController? ios ios

How To Dynamically change the contentSize of UIPopoverController?


I might be very late to answer but for new user from iOS 7 please use the following line in your UIViewController i,e contentViewController of your UIPopOverViewConotroller

-(void) viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];    self.preferredContentSize=myTableView.contentSize;}

Hope this will help for iOS 7 user.


Well, In the end i did something that I'm not sure if it's the right thing to do, but it is working.

I added a reference in my contentViewController to the popoverController:

@property (nonatomic , assign) UIPopoverController *popoverControllerContainer;

Then, I added the resizing code to viewWillAppear and viewDidAppear:

- (void)viewDidLoad{    [super viewDidLoad];    [self.tableView reloadData];}-(void) viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    self.contentSizeForViewInPopover = self.tableView.contentSize;}-(void) viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];    [self.popoverControllerContainer setPopoverContentSize:self.contentSizeForViewInPopover animated:YES];}

So, keeping a reference to the popover is kind of hack-ish, so I'm open to hear better ideas.


A UIViewController class has the property

self.contentSizeForViewInPopover

which will resize the pop over w/out needing to adding a reference to it.

And to expand on a solution, i used the method rectForSection: to get the size of the section (mine only has 1 section, so easy enough to get) and then added the height of the navigation bar (it seems to be 20). so i'm able to create the popover the size of the finished table view:

CGRect sectionRect = [view.tableView rectForSection:0];if (sectionRect.size.height + 20 < POPOVER_SIZE.height)    view.contentSizeForViewInPopover = CGSizeMake(POPOVER_SIZE.width, sectionRect.size.height + 20);else    view.contentSizeForViewInPopover = POPOVER_SIZE;

might prove more difficult with multiple sections, i didn't try it. should just be able to sum up the section heights, but there might be some spacing issues that i don't know about.