Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIStackView; Equal Spacing Between & Outside Elements

UIStackView is awesome, I love Equal Spacing Distribution. But how to achieve the same space also outside of elements dynamically? In my case all elements will have same ratio 1:1

enter image description here

like image 945
Adam Smaka Avatar asked Mar 27 '17 11:03

Adam Smaka


People also ask

What is fill proportionally in Uistackview?

case fillProportionally. A layout where the stack view resizes its arranged views so that they fill the available space along the stack view's axis. Views are resized proportionally based on their intrinsic content size along the stack view's axis.

What is Uistackview?

A streamlined interface for laying out a collection of views in either a column or a row.

What is Stackview alignment?

A layout where the stack view aligns the center of its arranged views with its center along its axis. case leading. A layout for vertical stacks where the stack view aligns the leading edge of its arranged views along its leading edge.


2 Answers

@Declan has the right idea. Here's that answer programatically where you add extra views on either side so the stack view gives correct outside spacing with any number of buttons.

stackView.alignment = .center
stackView.axis = .horizontal
stackView.distribution = .equalCentering

// Then when I add the views...
let leftView = UIView()
stackView.addArrangedSubview(leftView)
content.buttons.forEach { (button) in
  stackView.addArrangedSubview(button)
}
let rightView = UIView()
stackView.addArrangedSubview(rightView)

Here's what my view looks like with 2 items using equalSpacing

EqualSpacing UIStackView

And here it is with equalCentering distribution, also a nice look.

EqualCentering UIStackView

like image 167
teradyl Avatar answered Oct 07 '22 03:10

teradyl


I prefer to let the UIStackView handle the spacing. Create a UIStackView with equal spacing and add two 0px wide (0px high if using a vertical stackview) transparent views to the the far sides of your stack view.

View Layout Screenshot

StackView Properties Screenshot

like image 40
Declan McKenna Avatar answered Oct 07 '22 02:10

Declan McKenna