Xcode6Swift AutoLayout for dynamic items

Asked 1 years ago, Updated 1 years ago, 76 views

Xcode 6.4, Language: Swift is developing.
On the Storyboard, we create a vertical scroll page with UILabel and UIImage on top of ScrollView.
There are several UILabels that dynamically change the text of the label from the server through the API.
I set the AutoLayout constraints in order from the top, but depending on the conditions, I may not want to display some UILabels, so I don't know the AutoLayout settings.

UIImageView← Absolutely Present

UILabel_1 ← Absolutely Present

UILabel_2←Varies with or without

UILabel_3 ← Absolutely Present

UILabel_1
at 5px down from UIImageView UILabel_2
at 5px down from UILabel_1 UILabel_3

at 5px down from UILabel_2

If the View looks like the one above, and you set the AutoLayout constraint from the top to the next.
Without UILabel_2, the layout will be corrupted.

If the text of UILabel_2 is empty, hide=true or UILabel_2.text="", there will be extra gaps where UILabel_2 is located.


Location of UILabel_3 if text in UILabel_2 is empty I want to be 5px down from UILabel_1. (Packed image above)

How do I implement it?
Please let me know.

swift xcode xcode6 storyboard autolayout

2022-09-30 20:58

1 Answers

Connect label and top space constraints to an outlet and change the value of constant with or without text

@IBOutlet weak var label:UILabel!
@IBOutlet weak vartopSpaceConstraint:NSLayoutConstraint!

... ... ...

// After receiving text from the server, etc.

if(label.text!.isEmpty){
  topSpaceConstraint.constant=0
} else{
  topSpaceConstraint.constant=5
}


2022-09-30 20:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.