Created
May 31, 2024 02:25
-
-
Save robertmryan/3be455c999bf7ba1b1c581d54d06b21b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
UILayoutGuideobjects 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: