Week 3
Autolayout Constraints
Announcements
Lab 2 Released
Lab 2 has been released and will be due Monday, September 28 at 11:59 pm
iOS Decal Fall 2021
Closures
lazy
Autolayout Constraints
Demo
iOS Decal Fall 2021
Closures
lazy
Autolayout Constraints
Demo
iOS Decal Fall 2021
Closures
iOS Decal Fall 2021
Closures
lazy
Autolayout Constraints
Demo
iOS Decal Fall 2021
lazy
iOS Decal Fall 2021
lazy Initialization
iOS Decal Fall 2021
class ViewController: UIViewController {
lazy var testLabel: UILabel = {
let closureLabel = UILabel()
closureLabel.text = "Hello"
return closureLabel
}()
override func viewDidLoad() {
view.addSubview(testLabel)
}
}
lazy Initialization
iOS Decal Fall 2021
class ViewController: UIViewController {
lazy var testLabel: UILabel = {
let closureLabel = UILabel()
closureLabel.text = "Hello"
return closureLabel
}()
override func viewDidLoad() {
view.addSubview(testLabel)
}
}
This is a closure
closureLabel only exists inside the scope of the {}
When testLabel is referenced for the first time, the closure will run and the value of closureLabel will be assigned to testLabel
Closures
lazy
Autolayout Constraints
Demo
iOS Decal Fall 2021
Autolayout Constraints
iOS Decal Fall 2021
Autolayout Anchor System
iOS Decal Fall 2021
centerYAnchor
bottomAnchor
topAnchor
leadingAnchor
trailingAnchor
centerXAnchor
* Constants need to be negative
Constraint Examples
iOS Decal Fall 2021
label.translatesAutoresizingMaskIntoConstraints = false
label.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8).isActive = true
label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8).isActive = true
label.heightAnchor.constraint(equalTo: 50).isActive = true
label
8pts
8pts
50pts
Constraint Breakdown
iOS Decal Fall 2021
label.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
“Align the top edge of label with the top edge of the screen”
Constraint Breakdown
iOS Decal Fall 2021
label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8).isActive = true
“Align the leading edge of label 8 pts inside the leading edge of the screen”
Constraint Breakdown
iOS Decal Fall 2021
label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8).isActive = true
“Align the trailing edge of label 8 pts inside the trailing edge of the screen”
Constraint Breakdown
iOS Decal Fall 2021
label.heightAnchor.constraint(equalToConstant: 50).isActive = true
“Set the height of label to 50 pts”
.translatesAutoresizingMaskIntoConstraints = false
iOS Decal Fall 2021
Closures
lazy
Autolayout Constraints
Demo
iOS Decal Fall 2021