Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save robertmryan/3be455c999bf7ba1b1c581d54d06b21b to your computer and use it in GitHub Desktop.

Select an option

Save robertmryan/3be455c999bf7ba1b1c581d54d06b21b to your computer and use it in GitHub Desktop.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let label1 = addLabel(with: "I am a dynamic object")
let spacer1 = addSpacer()
let label2 = addLabel(with: "Me too.")
let spacer2 = addSpacer()
let label3 = addLabel(with: "Me three")
let spacer3 = addSpacer()
let label4 = addLabel(with: "hi.")
NSLayoutConstraint.activate([
label1.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
label1.bottomAnchor.constraint(equalTo: spacer1.topAnchor),
label2.topAnchor.constraint(equalTo: spacer1.bottomAnchor),
label2.bottomAnchor.constraint(equalTo: spacer2.topAnchor),
label3.topAnchor.constraint(equalTo: spacer2.bottomAnchor),
label3.bottomAnchor.constraint(equalTo: spacer3.topAnchor),
label4.topAnchor.constraint(equalTo: spacer3.bottomAnchor),
label4.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
spacer1.heightAnchor.constraint(equalTo: spacer2.heightAnchor),
spacer2.heightAnchor.constraint(equalTo: spacer3.heightAnchor)
])
}
func addLabel(with text: String) -> UILabel {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = text
label.textAlignment = .center
view.addSubview(label)
NSLayoutConstraint.activate([
label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
])
return label
}
func addSpacer() -> UILayoutGuide {
let spacer = UILayoutGuide()
view.addLayoutGuide(spacer)
return spacer
}
}
@robertmryan

robertmryan commented May 31, 2024

Copy link
Copy Markdown
Author

The UILayoutGuide objects have no view associated with them, so while they appear in the view debugger hierarchy in the panel on the left, there is no associated view in the main panel on the right:

CleanShot 2024-05-30 at 19 30 31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment