UIStackView; Equal Spacing Between & Outside Elements

You can add equal spacing using the story board as shown here:

Source: https://stackoverflow.com/a/32862693/3393964

enter image description here


@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