I am excited by the prospect of being able to build my static TableViews right within Xcode, using Auto Layout to support dynamic type, as mentioned in WWDC2014 What's New in Table and Collection Views.
I'd prefer to use strictly Xcode / Interface Builder and storyboards to handle the Auto Layout. When I do, somehow the TableView cells are not resizing and I'm sure I'm missing something simple.
To reproduce this, using Xcode 7 beta 5, I:
- Created a new single view project.
- Set the Storyboard entry point to a new UITableViewController.
- Make it a static table view with grouped style.
- Added a single label to the first row. Added constraints to the top, bottom, left, and right.
- Set the label font to be "Body".
Added to ViewDidLoad:
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0;
Ran it and adjusted Dynamic Type in Settings. While the font changes, the TableView height remains stuck at the apparent default of 44 and truncates the label.
Here is a GitHub repository which is the result of the above.
Why isn't Auto Layout causing the TableView to expand to fill the necessary space to fit the new larger label text?
I have found great posts on the subject such as the following, with a bent on using code to set up constraints here.
I'm not finding any examples of doing this in pure Interface Builder. Surely this is quite possible?
Update
I have updated the GitHub source to include a second table row with an ImageView instead of a UILabel; it shows the same issue:
Additionally, when I set an explicit height constraint on either the label or image view, I generate the following. The problem seems to be this "UIView-Encapsulated-Layout-Height" constraint which I have not set. I'm guessing it's generated because in my static tableview in IB, the Row Height is set (as default) to 44.
Unable to simultaneously satisfy constraints.
...
"<NSLayoutConstraint:0x7fa371f39e30 V:[UILabel:0x7fa371f38d30'Label'(20)]>",
"<NSLayoutConstraint:0x7fa371c39160 UILabel:0x7fa371f38d30'Label'.top == UITableViewCellContentView:0x7fa371e17dc0.topMargin>",
"<NSLayoutConstraint:0x7fa371c393c0 UITableViewCellContentView:0x7fa371e17dc0.bottomMargin == UILabel:0x7fa371f38d30'Label'.bottom>",
"<NSLayoutConstraint:0x7fa371f41a40 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fa371e17dc0(43.5)]>"
But shouldn't this in ViewDidLoad be overriding this?
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0;
Do self-sizing table view cells simply not work with static table views?
Update 2
I have built the same thing using a dynamic prototype instead of a static table view, and all works fine:
It does indeed seem to be static versus dynamic prototyped tables. Static table view cells do not properly expand.
I've seen no mention of dynamic vs. static in the WWDC videos, so at this point I'm chalking this up to a bug. Will update if I learn anything new.
I have updated the GitHub repository above to show an example of both the static (broken) and dynamic (working) table views.
See Question&Answers more detail:
os