简体   繁体   English

设置 UI 集合视图单元的约束

[英]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.

相关问题 为集合视图单元格设置自动布局宽度和高度限制的最佳做法? - Best practices for setting autolayout width and height constraints for a collection view cell? 在集合视图单元格中设置文本时出错 - Error setting text in collection view cell 在集合视图单元格中设置 UIView 的渐变背景 - Setting Gradient Background on UIView in Collection View Cell 将单元格添加到UI集合视图的底部 - Adding cell to bottom of UI Collection View 原型收集视图单元格“无法同时满足约束。”警告 - Prototyped collection view cell “Unable to simultaneously satisfy constraints.” warning 设置动态单元格的约束 - Setting constraints for dynamic cell 在集合视图单元格中添加的图像视图不会应用添加的约束 - Image view added in collection view cell doesn't apply added constraints 如何在快速按下集合视图单元格时修复自定义视图中的约束 - How to fix constraints in custom view when collection view cell pressed in swift 检测到约束模糊地暗示集合视图单元格的内容视图的大小为零的情况 - Detected a case where constraints ambiguously suggest a size of zero for a collection view cell's content view 在“集合”视图单元格内设置标签的值时发现nil - Found nil while setting value of a label inside Collection view cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM