user3766930
user3766930

Reputation: 5829

how can I use constraints on a group of elements in storyboard in ios?

in my UIStoryBoard I have the following situation:

enter image description here

a UIViewController with two elements - a button and a label. Text on each component is fetched from localized.strings, so it has different length each time user chooses different language. In the code I have:

myButton.setTitle("TextOnButton".localized(), for: .normal)
myButton.sizeToFit()
txtLabel.text = "txt".localized()

how should I attach constraints so that those two elements - when grouped together - are always in the center? For example:

this is not in the middle:

enter image description here

this is in the middle:

enter image description here

If it was only one element, I would attach constraint midX and that's all, but what about the situation when there are two elements? can I just - somehow - attach constraints to group of elements?

Upvotes: 0

Views: 2964

Answers (1)

jeanggi90
jeanggi90

Reputation: 761

You can achieve that with help of something called Stack View. In the StoryBoard select both labels and click the following button:
StackViewButton

In the hierarchy you will notice that both labels got wrapped inside a Stack View. Now you just have to add the Horizontally in Containerand Vertically in Containerconstraints to the stack view. Finally you can add some Spacing between the two labels in the attribute inspector when the stack view is selected.
Finally result:
Final result

Upvotes: 4

Related Questions