Search code examples
iosuitableviewautolayoutios-autolayout

UITableViewCell auto layout constraint problems due to separator line height


I have this rather simple UITableViewCell.

enter image description here

It has an Avatar view, the height of which is set to 34 points, with 10 points vertical spacing to the top and the bottom of the cell's contentView. The cell itself is set to use an automatic row height, and here I can already see a small problem: the row height value is set to 54.5.

enter image description here

And yea, when I inspect the Avatar view, it has a height of 34.5 even though it has a required constraint to be 34 points tall, and content hugging and compression resistance all set to 1000.

enter image description here

When I run the app on the iPhone 11 Pro simulator, I get the following warning in the console:

(
    "<NSLayoutConstraint:0x6000030a39d0 Jetfly.Avatar:0x7fabbb8e1310.height == 34   (active)>",
    "<NSLayoutConstraint:0x6000030a3f70 V:|-(10)-[Jetfly.Avatar:0x7fabbb8e1310]   (active, names: '|':UITableViewCellContentView:0x7fabbb8e1ab0 )>",
    "<NSLayoutConstraint:0x600003088d20 Jetfly.Avatar:0x7fabbb8e1310.centerY == UITableViewCellContentView:0x7fabbb8e1ab0.centerY   (active)>",
    "<NSLayoutConstraint:0x600003089ea0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fabbb8e1ab0.height == 54.3333   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000030a39d0 Jetfly.Avatar:0x7fabbb8e1310.height == 34   (active)>

And when I inspect my view hierarchy, I can see that the Avatar got a height of 34,33 points.

Okay, so it has obviously something to do with the separator line, which is 0.33 points on the 11 Pro (it's an @3x device, making it one pixel). I get that. But.. how do I add constraints that don't cause these warnings and more importantly, don't change the height of my Avatar view?


Solution

  • Lower the priority of one vertical-axis constraint to 999. I usually use the bottom constraint from the bottom-most view to the bottom of the content view.