GaTz
GaTz

Reputation: 27

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 nowenter image description here

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

Upvotes: 0

Views: 454

Answers (3)

elk_cloner
elk_cloner

Reputation: 2149

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

enter image description here enter image description here

After clicking hide.

enter image description here

After clicking show

enter image description here

Upvotes: 1

abhishek
abhishek

Reputation: 121

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.

Upvotes: 0

Alex Brown
Alex Brown

Reputation: 1613

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.

Upvotes: 1

Related Questions