Skip to content

Instantly share code, notes, and snippets.

@ChrisMarshallNY
Created June 1, 2020 15:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisMarshallNY/09a54c925637a0e7ee68c1d9cb245bde to your computer and use it in GitHub Desktop.
Save ChrisMarshallNY/09a54c925637a0e7ee68c1d9cb245bde to your computer and use it in GitHub Desktop.
UIView Extension to Add An Auto-Layout SubView With Equal All-Around Constraints (Fill)
extension UIView {
/* ################################################################## */
/**
This allows us to add a subview, and set it up with auto-layout constraints to fill the superview.
- parameter inSubview: The subview we want to add.
*/
func addContainedView(_ inSubView: UIView) {
addSubview(inSubView)
inSubView.translatesAutoresizingMaskIntoConstraints = false
inSubView.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
inSubView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0).isActive = true
inSubView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0).isActive = true
inSubView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0).isActive = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment