Skip to content

Instantly share code, notes, and snippets.

@josuesasilva
Forked from morishin/left-align-stack-view.png
Created July 11, 2019 15:14
Show Gist options
  • Save josuesasilva/8b6710b2481a90119c26570612a50239 to your computer and use it in GitHub Desktop.
Save josuesasilva/8b6710b2481a90119c26570612a50239 to your computer and use it in GitHub Desktop.
Align to Left the contents of UIStackView
import UIKit
import PlaygroundSupport
let view = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
view.backgroundColor = .white
let stack = UIStackView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
stack.axis = .horizontal
stack.alignment = .center
stack.distribution = .fill
stack.spacing = 8
let label = UILabel()
label.text = "ラベル"
label.backgroundColor = .blue
let button = UIButton(type: .roundedRect)
button.setTitle("ボタン", for: .normal)
button.backgroundColor = .green
let spacerView = UIView()
spacerView.setContentHuggingPriority(.defaultLow, for: .horizontal)
stack.addArrangedSubview(label)
stack.addArrangedSubview(button)
stack.addArrangedSubview(spacerView)
view.addSubview(stack)
PlaygroundPage.current.liveView = view
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment