Skip to content

Instantly share code, notes, and snippets.

@miguelfermin
Created December 10, 2018 11:16
Show Gist options
  • Save miguelfermin/b5f3137c98cf53c35e1f1525f87dc3f3 to your computer and use it in GitHub Desktop.
Save miguelfermin/b5f3137c98cf53c35e1f1525f87dc3f3 to your computer and use it in GitHub Desktop.
Simple function that adds a child view and its constraints
func addChildView(_ childView: UIView, to containerView: UIView) {
containerView.addSubview(childView)
childView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: childView,
attribute: .leading,
relatedBy: .equal,
toItem: containerView,
attribute: .leading,
multiplier: 1.0,
constant: 0.0).isActive = true
NSLayoutConstraint(item: childView,
attribute: .trailing,
relatedBy: .equal,
toItem: containerView,
attribute: .trailing,
multiplier: 1.0,
constant: 0.0).isActive = true
NSLayoutConstraint(item: childView,
attribute: .top,
relatedBy: .equal,
toItem: containerView,
attribute: .top,
multiplier: 1.0,
constant: 0.0).isActive = true
NSLayoutConstraint(item: childView,
attribute: .bottom,
relatedBy: .equal,
toItem: containerView,
attribute: .bottom,
multiplier: 1.0,
constant: 0.0).isActive = true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment