When is it preferable to respect ContentSize?

I have a table view controller that gets its contents after viewDidLoad . When setting up new content, I compute preferredContentSize . Before a popover view, I can request the preferredContentSize my view controller, which is correct. But after the presentation, I get a standard popover size (320x480). If I use setPopoverContentSize:animated: with previously requested values, everything works.

Now my question is: why doesn't it respect preferredContentSize in the beginning? What am I doing wrong?

+5
source share
2 answers

Now I had the same problem another time. If I put the table height calculation in viewWillAppear , how does it work:

 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.

+5
source

I converted the test code to Swift 2.0.

 override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) tableView.layoutIfNeeded() preferredContentSize.height = tableView.contentSize.height } 
+1
source

Source: https://habr.com/ru/post/1205706/


All Articles