<aside>
🛠 Make sure you have read and understood Layout — Building Views before reading this page.
</aside>
Constraints
- A scroll view should only have 1 child, a view that we'll call content view.
- All views inside the content view should have constraints between them and the content view, never with views outside the content view.
- We need a chained constraint starting at the top of the content view and ending at the bottom. If not, the content view won't have an explicit height and UIKit won't know how to render it correctly.
- Chained constraint means that all subviews link top <-> bottom between them.
- content view <-> first subview <-> second subview <-> ... <-> content view
let scrollView = UIScrollView()
let contentView = UIView()
view.addSubview(scrollView)
scrollView.addSubview(contentView)
contentView.addSubview(...)
scrollView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
contentView.snp.makeConstraints { make in
make.height.greaterThanOrEqualTo(safeArea) // Optional, but recommended
make.width.equalTo(view)
make.edges.equalToSuperview()
}