Skip to content

Instantly share code, notes, and snippets.

@Dev1an
Last active March 15, 2021 14:32
Show Gist options
  • Save Dev1an/bd82d0c0bc67359902718f70edf092af to your computer and use it in GitHub Desktop.
Save Dev1an/bd82d0c0bc67359902718f70edf092af to your computer and use it in GitHub Desktop.
let stack = UIStackView(arrangedSubviews: [])
stack.axis = .vertical
let marginGuide = UILayoutGuide()
stack.addLayoutGuide(marginGuide)
let hugger = marginGuide.widthAnchor.constraint(equalToConstant: 0)
hugger.priority = .defaultHigh
stack.addConstraint(hugger)
func addRow(label: String, text: String) {
let labelView = UILabel()
let textView = UILabel()
labelView.text = label
labelView.textAlignment = .right
labelView.numberOfLines = 1
textView.text = text
textView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
textView.numberOfLines = 0
let row = UIStackView(arrangedSubviews: [labelView, textView])
row.alignment = .leading
row.spacing = 5
stack.addArrangedSubview(row)
stack.addConstraint(marginGuide.widthAnchor.constraint(equalTo: labelView.widthAnchor))
row.setContentCompressionResistancePriority(.required, for: .vertical)
}
let button1 = UIButton(primaryAction: UIAction(title: "One") { action in
stack.backgroundColor = stack.backgroundColor == UIColor.red ? UIColor.systemBackground : .red
})
stack.addArrangedSubview(button1)
addRow(label: "Ref:", text: "Hello world this is me")
addRow(label: "Refrain:", text: "OK Cool this is super cool and it works like a charm")
addRow(label: "2.", text: "Something else")
return stack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment