Created
April 8, 2021 20:42
-
-
Save jong9000/a937da2ad5b02c6328c34a75b1b9c658 to your computer and use it in GitHub Desktop.
Basic example of creating iOS view and adding label with constraints programatically.
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
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