Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIStackView children equally spaced (around and between)

How can I have an UIStackView with the same space as padding and gap between views?

How can I achieve this layout:

Expected layout

When this one doesn't suit me:

No good layout

Neither does this:

enter image description here

I just need the around views space to be the same as the between views space. Why is it so hard?

Important I'm using my fork of TZStackView to support iOS 7. So no layoutMargins for me :(

like image 972
gfpacheco Avatar asked Oct 22 '15 21:10

gfpacheco


1 Answers

I know this is an older question, but the way I solved it was to add two UIViews with zero size at the beginning and end of my stack, then use the .equalSpacing distribution.

Note: this only guarantees equal around spacing along the main axis of the stack view (i.e. the left and right edges in my example)

let stack = UIStackView()

stack.axis         = .horizontal
stack.alignment    = .center
stack.distribution = .equalSpacing

// add content normally
// ...

// add extra views for spacing
stack.insertArrangedSubview(UIView(), at: 0)
stack.addArrangedSubview(UIView())

enter image description here

like image 183
Jayson Avatar answered Sep 28 '22 01:09

Jayson