Search code examples
iosswiftautolayoutuitableviewtoday-extension

Wrong tableview.contentSize when I used it in Today Extension


I developed a "Today Extension" that shows data in a TableView. In this table use dynamic size to automatically calculate the height of the cells.

Then set preferredContentSize of today extension:

self.preferredContentsize = self.tableView.contentSize

The fact is that tableView.contentSize returns an incorrect value.

In my storyboard I set the constraints properly, I'm sure.

What can I try?


Solution

  • Set all pin constraints for top-bottom-left-right.

    Go to Storyboard -> Select UITableViewCell then tap on Pin button, which is at right bottom of Storyboard.

    Select all four pin constrains as shown in image below and click on Add 4 Constraints.

    enter image description here

    EDIT:

    You can do the same for xib files also. It is autoresizing concept.

    Select cell in .xib file, open right side Utilities pan, click on "Show the size inspector".

    Check below image, you can see four arrows are marked same like in Auto layout. By doing this you can achieve like above.

    enter image description here