复杂的自动版式与动态大小细胞 [英] Complex AutoLayout for Cell with dynamic size

查看:127
本文介绍了复杂的自动版式与动态大小细胞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含10个的子视图(其中两个是简单边界和其他为标签)的容器的细胞。该计划看起来是这样的。

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.

在哪里蓝色的观点是查看℃的子视图即可。后面的灰色视图是一个视图B 。粗体标签是静态的,其他的是动态的。

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.

演示项目

如何设置限制?

非常感谢你提前!

推荐答案

我设置管理的限制,让你得到你所需要的结果。这是我得到:

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.


  • 首先,你有需要是一样大的内容查看视图B。为此,添加上/下/左/右约束到上海华。因为你是使用自动尺寸,如果优先级1000(最大的)添加的所有限制,你会得到一些错误的制约运行时。这是因为,细胞大小之前可以自动计算它是零,所以限制崩溃。因此,我设置的顶部和尾部空间的优先级和999的优先级,这样你就不会看到错误日志了。结果是相同的。

  • 然后查看C需用(顶部或底部)/左/右和高度约束

  • 然后你需要添加约束的标签。既然你需要合适的人拥有多条线路,约束需要为这种特殊情况下指定垂直布局。所以,你有如下:第一个标签:顶/左名称标识权和超级视图。所有其他的都顶到了previous之一,下到下一个。

  • 对于不需要调整只需导致空间父,水平空间到右标签和静态宽度的标签。此外,您将需要一个约束对准顶部右侧的标签。

  • 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!

这篇关于复杂的自动版式与动态大小细胞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆