UIStackView inside UISScrollView

1. Disable tAMIC

[cc lang=”objc”]
scrollView.translatesAutoresizingMaskIntoConstraints = false
stackView.translatesAutoresizingMaskIntoConstraints = false
[/cc]

2. Pin the ScrollView

[cc lang=”objc”]
NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: trailingAnchor),
scrollView.topAnchor.constraint(equalTo: topAnchor),
scrollView.bottomAnchor.constraint(equalTo: bottomAnchor)
])
[/cc]

3. Pin the StackView to the ScrollView corners. Include minimum width + padding:

[cc lang=”objc”]
let padding = CGFloat(10)
NSLayoutConstraint.activate([
stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: padding),
stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: -padding),
stackView.topAnchor.constraint(equalTo: scrollView.topAnchor),
stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
stackView.heightAnchor.constraint(equalTo: scrollView.heightAnchor),
stackView.widthAnchor.constraint(greaterThanOrEqualTo: scrollView.widthAnchor, constant: -2 * padding)
])
[/cc]