Complex AutoLayout for Cell with dynamic size

﹥>﹥吖頭↗ 提交于 2019-12-08 09:54:24

问题


I have a cell that contains a container with 10 subviews (two of them are simply bounds and the others are labels). The scheme looks like this.

Dynamic labels may contain huge text so the cells should conform the appropriate size to fit the content. The question is how to set up all the constraints manually... I've tried a dozen of times to do it myself but seems I'm not that good at this. The table view supports auto dimension for row height and uses custom estimated height.

In Storyboard it looks this way.

Where blue views are a subviews of View C. A grey view behind is a View B. Bold labels are static and the others are dynamic.

Demo project.

How to setup constraints?

Thank you very much in advance!


回答1:


I managed to setup your constraints so that you get the result you needed. This is what I get:

I hope this is how you wanted it to look like.

Here is a link with the project. I will try to explain how I added the constraints so that it makes more sense.

  • First of all, you have view B which needs to be as big as the contentView. For this you add top/bottom/left/right constraints to the superView. Because you are using automatic dimensions, if you add all constraints with priority 1000(the maximum one), you will get some error with the constraints while running. This is because, before the cell size can be calculated automatically it is zero, so the constraints crash. Therefore, I set the priority for top and trailing space with a priority of 999 so that you don't see the error log anymore. The result is the same.
  • Then views C needs (top or bottom)/left/right and height constraint
  • Then you need to add the constraints for the labels. Since you need the right ones to have multiple lines, the constraints need to specify the vertical layout for this particular case. So, you have as follows: first label: top/left to name label and right to super view. All the other have top to the previous one,and bottom to the next one.
  • for the labels that don't need to resize you just need leading space to parent,horizontal space to the right label and static width. Also, you will need a constraint to align the top with the label on the right.

This is the result I get:

Hope my explanation made sense, just let me know if you have questions. Good luck with your project!



来源:https://stackoverflow.com/questions/30268865/complex-autolayout-for-cell-with-dynamic-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!