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:
iOS8/9 solution - just override
preferredContentSize
and force the table view to layout prior to returning it'scontentSize
.A UIViewController class has the property
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:
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.
For iOS 7 or above.
If you are a child of a container, re-direct the content size to yourself. E.g. In a
UINavigationController
subclass: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:
Then, I added the resizing code to viewWillAppear and viewDidAppear:
So, keeping a reference to the popover is kind of hack-ish, so I'm open to hear better ideas.
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
Hope this will help for iOS 7 user.
My app has a menu with sub-menus originating from a popover and embedded in a navigation controller, i.e. something like
UINavigationController
->TopMenuViewController
->SubMenuViewController
xN.None of the above solutions worked for me.
My solution: In my root menu view controller class, from which all other menu view controllers inherit, add the following code: