简体   繁体   中英

UITableView auto resizing row constraint breaking mysteriously on iPhone 6Plus

I have a custom UITableViewCell which has a thumbnail and bunch of text. The row height is configured to be calculated automatically using

tableView.estimatedRowHeight = 129;
tableView.rowHeight = UITableViewAutomaticDimension

The row height should be calculated as exactly 138 points. Everything looks great on the iPhone 5. However, on iPhone 6 Plus, the auto row height fails INTERMITTENTLY for random rows with the following log.

(
    "<NSLayoutConstraint:0x17009ddd0 V:|-(20)-[scoop.ThumbnailImage:0x124d2a5a0]   (Names: '|':UITableViewCellContentView:0x124e23200 )>",
    "<NSLayoutConstraint:0x17009de70 UITableViewCellContentView:0x124e23200.bottomMargin == scoop.ThumbnailImage:0x124d2a5a0.bottom + 20>",
    "<NSLayoutConstraint:0x17009e780 V:[scoop.ThumbnailImage:0x124d2a5a0(90)]>",
    "<NSLayoutConstraint:0x17009ef00 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x124e23200(138.333)]>"
)

The last line of the log seems to say that for some reason the row height was calculated as 138.333 instead of 138 . I have been banging my head for a while now but I am unable to figure out why this is happening. Can some one please help?

Update: This is how my table view cell looks like.

细胞

UPDATE I couldn't get the code out of the main repo since its a part of a bigger project. But I have managed to reproduce the issue with a very simple sane project. Please find it here on github .

This warning is telling you there's a conflict in your constraints. Reduce the priority of the height constraint to 999 and it will go away. Tested it in your Github project and worked perfectly.

在此处输入图片说明

0.333 on a 3x scale display (which iPhone 6+ is) is probably connected with the cell separator.

Note that your constraints don't set up the size of the cell, they set up the size of the contentView . But the cell has to add 2 pixels (= 0.666 points) to the cell height for the cell separator. Autolayout tries to keep view positions on integer boundaries so adding 0.666points to the cell height can result in adding 0.333 to your content height.

You can avoid the error by setting your table separators to None . Although setting up one of the priorities to 999 (usually the bottom priority) as the other answer has suggested is a good solution in general.

The warning is telling you exactly what the problem is, but you may not realize it. The first three constraints are for a 90 pixel-high image that's 20 pixels below the top of its container, and 20 pixels above its container's bottom margin . That's 130, and that's not compatible with the fourth constraint, which wants a total height of 138. However, because the bottom edge constraint is relative to the container's margin , that adds a certain number of pixels more. Either remove the total height constraint (my recommendation), or change the top and bottom edge amounts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM