[英]Setting the constraints of a UI Collection View Cell
In my UI there is a collection View
where each cell contains a UILabel
and a UIImageView
.在我的 UI 中有一个collection View
,其中每个单元格都包含一个UILabel
和一个UIImageView
。 There are aligned as follows.排列如下。
Where I am stuck is setting constraints to them because each UILabel has a random length.我被卡住的地方是对它们设置约束,因为每个 UILabel 都有一个随机长度。 I always want to make the distance between left side of the image and left side of the cell equal to the distance between the right side of the label and the right side of the cell (in a certain cell).我总是想让图像左侧和单元格左侧之间的距离等于 label 右侧和单元格右侧之间的距离(在某个单元格中)。
I know I can set the centerY
of each element equals to centerY
of each element.我知道我可以将每个元素的centerY
设置为每个元素的centerY
。 But how can I set the other anchor.但是我怎样才能设置另一个锚。
I am not Using Storyboards.我没有使用故事板。
I would add a horizontal stack view, set constraints to every edge with the desired margin, and add the image and label to the stack view.我将添加一个水平堆栈视图,为每个边缘设置具有所需边距的约束,并将图像和 label 添加到堆栈视图中。
Then set the hugging priority to the image view higher than the label.然后将拥抱优先级设置为高于 label 的图像视图。 This will cause the label to take up the remaining space while being aligned to the left.这将导致 label 在向左对齐的同时占用剩余空间。
If I understand your problem correctly, you want to set spacing for left side of UImageView and right side of UILabel equally.如果我正确理解您的问题,您希望为 UImageView 的左侧和 UILabel 的右侧设置间距。 For this, I would recommend you to put those views (UIImageView and UILabel) in a UIStackView with properties: axis as "Horizontal" and Alignment as "Center";为此,我建议您将这些视图(UIImageView 和 UILabel)放在具有属性的 UIStackView 中:轴为“水平”,Alignment 为“中心”; and adding constraint for Leading and Trailing Space with CenterX and CenterY Constraint.并使用 CenterX 和 CenterY 约束为前导和尾随空间添加约束。
For ease of understanding take a look at the image of UIStackView Constraints为了便于理解,请查看UIStackView 约束的图像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.