Add alternate approach of using anchor constraints

This commit is contained in:
CypherPoet 2019-01-19 00:57:55 -05:00
parent f49f047c86
commit ef2ce34db1
1 changed files with 20 additions and 2 deletions

View File

@ -27,8 +27,9 @@ class ViewController: UIViewController {
// Do any additional setup after loading the view, typically from a nib.
createViews()
addHorizontalViewConstraints()
addVerticalViewConstraints()
// addHorizontalViewConstraints()
// addVerticalViewConstraints()
addAnchorConstraints()
}
@ -100,6 +101,23 @@ class ViewController: UIViewController {
)
)
}
func addAnchorConstraints() {
for labelNumber in 1...labelViews.count {
let label = labelViews["label\(labelNumber)"]!
label.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
label.heightAnchor.constraint(equalToConstant: 88).isActive = true
if labelNumber > 1 {
let labelAbove = labelViews["label\(labelNumber - 1)"]!
// create a topAnchor constraint if we have a previous label (and thus, a bottomAnchor to offset from)
label.topAnchor.constraint(equalTo: labelAbove.bottomAnchor, constant: 10).isActive = true
}
}
}
}