Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
extension UIStackView {
/**
Fills the given scroll view with this stack view. This method automatically creates
and activates constraints and adds the stack view to the scroll view.
- parameter scrollView: The scroll view into which this stack view should be inserted.
*/
internal func attachToScrollView(_ scrollView: UIScrollView) {
// Add this stack view to the given UIScrollView
scrollView.addSubview(self)
// Disable automatic constraints from autoresizing mask
self.translatesAutoresizingMaskIntoConstraints = false
// Create and activate constraints
self.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
self.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
self.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
self.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
self.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment