Kobe Kobe - 1 month ago 20
Swift Question

How to add leading padding to view added inside an UIStackView

This is my setup: I have an

UIScrollView
with leading,top, trialing edge set to 0. Inside this I add an
UIStackView
with this constraints:

stackView.centerYAnchor.constraintEqualToAnchor(selectedContactsScrollView.centerYAnchor).active = true
stackView.leadingAnchor.constraintEqualToAnchor(selectedContactsScrollView.leadingAnchor).active = true


Inside the stack view I add some views.

My issue is that because of the constraints the first view added to stack view will also have leading edge = 0.

What are the ways that I could add some padding to the first view ? Without adjusting the scroll view constraints.

Answer

The solution you have provided doesn't add a padding for your views inside your UIStackView (as you wanted in the question), but it adds a leading for the UIStackView.

A solution could be to add another UIStackView inside your original UIStackView and give the leading to this new UIStackVIew. Then, add your views to this new UIStackView.

Hint, you can do that completely using Interface Builder. In other words, no need to write code for it.

Comments