Reputation: 5829
in my UIStoryBoard
I have the following situation:
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:
this is in the middle:
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
Reputation: 761
You can achieve that with help of something called Stack View
. In the StoryBoard select both labels and click the following button:
In the hierarchy you will notice that both labels got wrapped inside a Stack View
. Now you just have to add the Horizontally in Container
and Vertically in Container
constraints 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:
Upvotes: 4