[英]Auto leading space between two elements in Swift
I have 3 hidden Labels, after a check from database i set label.isHidden = false
only if the value of that label exists. 我有3个隐藏的Labels,从数据库中进行检查后,仅当该Label的值存在时,我才设置label.isHidden = false
。
This is the situation when all labels have right value 所有标签都具有正确值时就是这种情况
So I'm using Storyboard with AutoLayout for determinate the position of these labels but i need to pull labels to left if one of these still hidden keeping the space with other labels. 因此,我将Storyboard与AutoLayout一起使用来确定这些标签的位置,但是如果其中一个标签仍然隐藏,则需要将标签向左拉,以保持与其他标签的空间。
This is how looking now 这就是现在的样子
Is it possible to do it programmatically? 是否可以通过编程方式进行? If yes, how? 如果是,怎么办?
One way to achieve this would be to align each label relative to the adjacent label (label1.trailing to label2.leading for example). 实现此目的的一种方法是将每个标签相对于相邻标签对齐(例如,label1.trailing到label2.leading)。
Then instead of setting any label to hidden you can set the width constraint to 0 and call layoutIfNeeded
on the superview, all other labels will then layout accordingly. 然后,可以将宽度约束设置为0并在超级视图上调用layoutIfNeeded
,而不是将任何标签设置为隐藏,然后所有其他标签将相应地进行布局。
Use StackView to handle this Space instead of using isHidden to hide the label. 使用StackView来处理此Space,而不要使用isHidden隐藏标签。
You can also achive that by AutoLayout, use yourConstraint.constant = 0 or priority property of the constraints. 您还可以通过AutoLayout实现,使用yourConstraint.constant = 0或约束的优先级属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.