Martin
Martin

Reputation: 5452

Size Classes in UITableViewCell

In my iOS 8 app, I have a custom UITableViewCell which is defined in a .xib file using autolayout. I have set up the UITableView so that it uses self-sizing cells, and that is all working just fine.

In the .xib file for my cell, I have a design for the "Any x Any" size class, which works. I also wanted a different design for the "Compact x Any" size class, so I added that layout to my .xib.

When I run the app on an iPhone device (real or simulator) I find that the cell is always sized as if it's using my "Any x Any" layout, and yet the content of the cell is always laid out as if it's using my "Compact x Any" size class. This happens regardless of orientation.

On an iPad device (real or simulator) it always uses the "Any x Any" layout for sizing and layout, which I think is correct.

Has anybody else tried this and got it to work on an iPhone? It doesn't feel like what I'm trying to do should be an unusual requirement.

In summary, I think the layout is happening correctly on all devices, but the self-sizing cell logic appears not to respect the size class it's in.

Upvotes: 2

Views: 2622

Answers (1)

Martin
Martin

Reputation: 5452

It seems that this is a known bug, described here: rdar://17799811.

The workaround there is to call tableView.reloadData() in my view controller's viewDidAppear method. It does work, but there's an ugly redraw where you see the squashed view content momentarily before it is repainted at the correct size.

In addition, on an iPhone 6 Plus, the vertical size class changes from compact to regular when you change orientation from portrait to landscape, so I also need to redraw when this happens. I could probably catch the orientation change, but instead (and I think this is the more "correct" approach) I chose to override the newer traitCollectionDidChange method in my view controller. I simply call tableView.reloadData() in there. In this case the change happens quite smoothly and I'm happy with it. It's just the initial ugly redraw when the table view is first shown that irks me.

Update: I found that relaxing the priority of one of my vertical constraints to 999 made the repaint considerably less ugly. The cell still changes its height quite noticeably, but at least the contents do not change size.

Upvotes: 4

Related Questions