Cesare
Cesare

Reputation: 9419

Spacing objects inside stack views

I put two labels inside a stack view and created a vertical spacing constraint between them to space them out a little. Though, this results in a conflict.

The reason why I'm using a stack view is because I want to pack multiple objects into a single one and then center the stack view to the screen. Why is this happening?

enter image description here

Upvotes: 0

Views: 206

Answers (2)

Jelly
Jelly

Reputation: 4522

The whole point of the stack view is to replace the need of using this kind of constraints. You can set the spacing of the object by selecting the stack view in the storyboard and go to attributes inspector in the right panel. There is a spacing parameter that you can play with and does what you need.

Upvotes: 1

matt
matt

Reputation: 535118

created a vertical spacing constraint between them

That's the problem. What do you think a stack view does? It creates constraints on its arranged subviews. You can't add another constraint manually. You must let the stack view do all the work. If you don't want to do that, don't use a stack view at all.

UIStackView itself provides for spacing. For example, you can use its Equal Centering distribution and set its spacing property. But if you aren't willing to use the stack view's settings to configure its arranged subviews' layout, then you must abandon your use of UIStackView altogether. Do not attempt to "mix and match" as you are doing now.

Upvotes: 1

Related Questions