Created
November 29, 2016 14:28
-
-
Save Blackjacx/fe74fdbbd7dcb9456381be191cfcc7ff to your computer and use it in GitHub Desktop.
[iOS] Adding a subview maximized with insets
This file contains 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 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