I have a table view controller which get it's content set after viewDidLoad
. When setting the new content I calculate the preferredContentSize
. Before I present the popover I can query the preferredContentSize
of my view controller, which is correct. But after the presentation I get the standard size of the popover (320x480). If I use setPopoverContentSize:animated:
with the before queried values everything works.
My question now is why doesn't it respect the preferredContentSize
right in the beginning? What I'm doing wrong?
No need to set preferredContentSize except when the size actually changes ✅ If you make the width and height constraints have priority = . defaultHigh , no unsatisfiable constraint warnings clogging your logs 💪
Self-Sizing A Popover To Fit The Content The preferredContentSize allows a child view controller to tell a parent container view controller the size it wants to be. In this case the parent container view is a popover presentation controller.
Now I had the same problem another time. If I put my table height calculation into viewWillAppear
than it works:
public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
TableView.LayoutIfNeeded ();
this.PreferredContentSize = new SizeF (320f, TableView.ContentSize.Height);
}
The code is in C# but you can easily convert it to Objective-C or Swift.
I converted testing’s code to Swift 2.0.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
tableView.layoutIfNeeded()
preferredContentSize.height = tableView.contentSize.height
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With