I\'ve looked through many answers and they all seem very complex! Most recently I was looking at this answer although I\'d prefer not to have to put my buttons inside views.
I just cooked this up in Swift using Cartography.
func disributeEvenlyAcrossWithCartography(views: [UIView], enclosingBox: UIView, spacer: CGFloat = 10.0) {
var priorView = UIView() // never null
for (index, view) in views.enumerate() {
constrain(view, priorView, enclosingBox) { view, prior, enclosingBox in
view.height == enclosingBox.height
view.centerY == enclosingBox.centerY
if index == 0 {
view.width == enclosingBox.width / CGFloat(views.count) - spacer
view.left == enclosingBox.left
} else {
view.left == prior.right + (spacer + spacer / CGFloat(views.count - 1))
view.width == prior.width
}
}
priorView = view
}
}
Result with 5 views and a small spacer: