简体   繁体   中英

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.

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.

This is how looking now 在此处输入图片说明

Is it possible to do it programmatically? If yes, how?

Use StackView for this. You don't have to do tiring auto-layout if you use StackView.Rather you will find what you want

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

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).

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.

Use StackView to handle this Space instead of using isHidden to hide the label.

You can also achive that by AutoLayout, use yourConstraint.constant = 0 or priority property of the constraints.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM