我在 UITableView
中使用自定义单元格.单元格中有 4 个视图.3 个 UILabel
和 1 个 UIImageView
(灰色一个),如下图所示.标签的文本是动态的,因此宽度和高度是动态的.
I am using custom cell in UITableView
. There are 4 views in cell. 3 UILabel
s and 1 UIImageView
(grey colored one) as shown below. Text of labels are dynamic so width and height is dynamic.
这里 UIImageView
的宽度取决于 2 个标签.所以问题是指定标签和 UIImageView
之间的约束,这将决定 UIImageView
的宽度.
Here width of UIImageView
depends on 2 labels. So issue is to specify constraints between labels and UIImageView
that will decide the width of UIImageView
.
看看下面如果第一个标签的文本很长会发生什么.
See below what happens if first label's text is long.
当宽度取决于多个标签的宽度时如何指定约束?
How to specify constraints when width depends on width of multiple labels?
更新:我尝试将标签的行数设置为 0,将固有大小设置为占位符.它也不起作用.下面是它的外观.我尝试将固有宽度和高度设置为无,但是当我为两个标签都这样做时会出错,如果只为一个标签设置则不会出错.
Update : I tried setting number of lines for labels to 0 and intrinsic size to placeholder. It's not working, too. Below is how it looks. I tried setting intrinsic width and height to none, but it gives error when I do that for both label, not giving error if set that for only one label.
更新:我更改了属性,下面显示了它们现在是什么以及单元格的显示方式.
Update : I changed properties and below is displayed what are they now and how the cell is displayed.
单元格:
第一行标签的约束(粉红色):
第二行标签的约束(青色):
我在尝试了很多东西后解决了这个问题.我唯一要做的就是将水平和垂直内容压缩阻力优先级设置为必需.即1000.我对所有标签都这样做了,因为我不希望任何标签修剪它们的内容.
I solved this after experimenting with lot of things. The only thing I had to do is to set horizontal and vertical content compression resistance priority to required.i.e. 1000. I did this for all labels because I don't want any of the labels to trim their content.
另外一件非常重要的事情是获得正确的单元格高度.如果在计算自定义单元格的高度时甚至出现 1pt 的错误,它将不会按预期显示.
One more thing which is too much important is Getting Right Height Of Cell. If there is even 1pt of error in calculating custom cell's height it will not be displayed as expected.
提示:
Yoy 可以通过向您为单元格计算的高度(变量)添加/删除常数值来测试高度是否错误.
Yoy can test if height is wrong by adding/removing constant value to height (variable) you calculate for cell.
这篇关于指定视图大小何时取决于多个子视图的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!