Skip to content

Instantly share code, notes, and snippets.

@jong9000
Created April 8, 2021 20:42
Show Gist options
  • Save jong9000/a937da2ad5b02c6328c34a75b1b9c658 to your computer and use it in GitHub Desktop.
Save jong9000/a937da2ad5b02c6328c34a75b1b9c658 to your computer and use it in GitHub Desktop.
Basic example of creating iOS view and adding label with constraints programatically.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func loadView() {
view = UIView()
view.backgroundColor = .white
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.textAlignment = .center
label.text = "hello, world"
view.addSubview(label)
NSLayoutConstraint.activate([
label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
label.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment