Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Dynamically change the contentSize of UIPopoverController?

I have a UIViewController that contains a UITableView. This UIViewController is being displayed in a UIPopoverController.

Now, the things is that the number of items in the tableView is not constant, and I want the size of the popover (that is - the popoverContentSize), to adjust according to the number of items in the tableView

Naively, I was thinking that if I'll set the contentSizeForViewInPopover in viewDidLoad after I'm loading the tableView with all the items - It'll do it.

It didn't.

So the make it short, my question is: How can I change the popoverContentSize directly from the contentViewController - after it's been presented?

Appendix: enter image description here

like image 467
Avi Shukron Avatar asked Dec 15 '11 07:12

Avi Shukron


2 Answers

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.

like image 77
Avinash Avatar answered Oct 14 '22 14:10

Avinash


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.

like image 9
Avi Shukron Avatar answered Oct 14 '22 13:10

Avi Shukron