Created
December 10, 2018 11:16
-
-
Save miguelfermin/b5f3137c98cf53c35e1f1525f87dc3f3 to your computer and use it in GitHub Desktop.
Simple function that adds a child view and its constraints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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