Skip to content

Instantly share code, notes, and snippets.

@Blackjacx
Created November 29, 2016 14:28
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 Blackjacx/fe74fdbbd7dcb9456381be191cfcc7ff to your computer and use it in GitHub Desktop.
Save Blackjacx/fe74fdbbd7dcb9456381be191cfcc7ff to your computer and use it in GitHub Desktop.
[iOS] Adding a subview maximized with insets
func addSubviewMaximized(subview: UIView, insets: UIEdgeInsets? = nil) {
let insets = insets ?? UIEdgeInsetsZero
let constraints: [NSLayoutConstraint] = [
subview.leftAnchor.constraintEqualToAnchor(leftAnchor, constant: insets.left),
subview.leftAnchor.constraintEqualToAnchor(leftAnchor, constant: -insets.right),
subview.topAnchor.constraintEqualToAnchor(topAnchor, constant: insets.top),
subview.bottomAnchor.constraintEqualToAnchor(bottomAnchor, constant: -insets.bottom),
]
subview.translatesAutoresizingMaskIntoConstraints = false
addSubview(subview)
// Activate all NSLayoutConstraints
NSLayoutConstraint.activateConstraints(constraints)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment