Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is preferredContentSize respected?

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?

like image 378
testing Avatar asked Oct 28 '14 15:10

testing


People also ask

When should you set preferredContentSize?

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 💪

What is preferredContentSize?

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.


2 Answers

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.

like image 87
testing Avatar answered Sep 25 '22 15:09

testing


I converted testing’s code to Swift 2.0.

override func viewWillAppear(animated: Bool) {
   super.viewWillAppear(animated)
   tableView.layoutIfNeeded()
   preferredContentSize.height = tableView.contentSize.height
}
like image 35
Smilk B Avatar answered Sep 22 '22 15:09

Smilk B