Created
October 18, 2017 13:31
-
-
Save BrandonShega/8218eca44b1355eb3b65eb4aca630c8d to your computer and use it in GitHub Desktop.
StackViews with Buttons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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