Skip to content

Instantly share code, notes, and snippets.

@BrandonShega
Created October 18, 2017 13:31
Show Gist options
  • Save BrandonShega/8218eca44b1355eb3b65eb4aca630c8d to your computer and use it in GitHub Desktop.
Save BrandonShega/8218eca44b1355eb3b65eb4aca630c8d to your computer and use it in GitHub Desktop.
StackViews with Buttons
//: Playground - noun: a place where people can play
import Foundation
import UIKit
import PlaygroundSupport
let buttonTitles = [["shortTitle", "longerTitle", "short"], ["reallyLongTitle", "short", "short"],["short", "short", "short"]]
let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 300.0, height: 300.0))
containerView.backgroundColor = .white
let stackView = UIStackView()
stackView.axis = .vertical
stackView.distribution = .fillEqually
stackView.alignment = .center
stackView.spacing = 10
stackView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(stackView)
containerView.addConstraints(
NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[stackView]-20-|",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: nil,
views: ["stackView": stackView])
)
containerView.addConstraints(
NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[stackView]-20-|",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: nil,
views: ["stackView": stackView])
)
for row in buttonTitles {
let buttonStackView = UIStackView()
buttonStackView.axis = .horizontal
buttonStackView.distribution = .equalSpacing
buttonStackView.alignment = .center
buttonStackView.spacing = 10
for title in row {
let button = UIButton(type: .custom)
button.backgroundColor = .black
button.setTitle(title, for: .normal)
buttonStackView.addArrangedSubview(button)
}
stackView.addArrangedSubview(buttonStackView)
}
PlaygroundPage.current.liveView = containerView
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment