Conflicting constraints with fixed size UIImageView and dynamic height UILabel in self-sizing UITableViewCell

时间:2017-08-13 13:49:23

标签: ios autolayout uistackview

I'm trying to create a relatively simple self-sizing UITableViewCell with a UIStackView. The cell will end up similar to the default UITableViewCellSubtitle. It has a UIImageView that should be sized exactly 64x64, and next to it, a dynamic-height UILabel (numberOfLines == 0, dynamic type, preferredMaxLayoutWidth set to automatic). The height of the cell should be at least as tall as the UIImageView, but taller if the text in the UILabel is taller than the UIImageView.

This is what the cell should look like: ![How the cell should look

I'm getting conflicting constraints, and I'm unable to reason why. The UIStackView's axis is horizontal, with alignment leading top and distribution fill. The UIImageView has an explicit width and height. The UILabel has no constraints. The UIStackView is set to align all four sides to the margins of the contentView.

Constraints and conflicts

If the cell has a static height, things are fine, though I must also manually measure the length of the string in the UILabel with boundingRectWithSize: and implement heightForRowAtIndexPath:. I'd much rather let the view system figure it out for me.

My assumption is that there's something awry with my hugging/compression properties. I have tried setting the UIImageView's hugging and compression resistance priorities to 1000, because I do not want it to grow or shrink larger than the explicit size I've set. I've tried setting the UILabel's hugging priorities low and compression resistance high, because I want the UILabel to be sized exactly as large as the text. I've tried changing the width constraint on the UIImageView to be <= (not ==) a value, which removes the ambiguity, but shrinks the UILabel to be one line, despite hugging/compression resistance properties. I've additionally tried without the UIStackView, and setting the height of the UILabel to >= the height of the UIImageView as well as the bottom of the UILabel to the bottom of the contentView with similar complications.


Update: I am using Xcode 9.0b5 under iOS 11. This same situation works as expected under Xcode 8.3.3/iOS 10. In Xcode 9.0b5, I have the "Automatic" checkboxes checked for rowHeight and estimatedRowHeight in the UIStoryboard. I've filed this as rdar://33867871

Update: Still present in 9.0 GM.

Update (11 Dec 2018): Still present in Xcode 10.1, though I just received a message that the bug duped by my bug repoert (rdar://33867871) has been closed. Crossing my fingers for the next release of Xcode.

0 个答案:

没有答案