简体   繁体   English

Swift中两个元素之间的自动行距

[英]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? 如果是,怎么办?

Use StackView for this. 为此使用StackView You don't have to do tiring auto-layout if you use StackView.Rather you will find what you want 如果您使用StackView,则不必累赘自动布局,而是可以找到想要的东西

在此处输入图片说明 在此处输入图片说明

After clicking hide. 单击隐藏后。

在此处输入图片说明

After clicking show 单击显示后

在此处输入图片说明

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.

相关问题 将Leading space设置为UIView,立即崩溃 - Set Leading space to UIView , Crashes in swift 如果两个元素并排放置在 swift 中,如何管理底部空间? - How to manage bottom space if two elements are placed side by side in swift? 如何在彼此不相邻的两个元素之间添加拖尾/前导约束 - How to add tailing/leading constraint between two elements which aren't next to each other Swift 初学者 - 自动布局约束(Xcode Interface Builder)问题 - 'leading = label.leading' 与 'label.leading =leading' 相同 - Swift Beginner - Auto layout constraints (Xcode Interface Builder) question - is 'leading = label.leading' the same as 'label.leading = leading' 在Swift中的tableviewcell之间添加空间 - Adding space between tableviewcell in Swift 在Swift 3中的单元格之间添加空间 - Add space between cells in Swift 3 如何使用自动布局在两个视图之间设置空间(在纵横比中) - How to set space between two view using auto layout(in Aspect Ratio) 使用Swift自动在UITextField之间切换 - Auto switching between UITextFields with Swift UILabel - 如何在Swift 3中的行之间添加空格 - UILabel - How to add space between lines in Swift 3 如何在 UICollectionView swift 中删除部分之间的空间 - How to remove space between section in UICollectionView swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM