Skip to content

Instantly share code, notes, and snippets.

@jonblatho
Last active June 24, 2019 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonblatho/01b4ea0388881e7f7049957ff3a298a2 to your computer and use it in GitHub Desktop.
Save jonblatho/01b4ea0388881e7f7049957ff3a298a2 to your computer and use it in GitHub Desktop.
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