Created
September 10, 2020 09:24
-
-
Save NikhilManapure/1eeb64205a0df0dcbb45705c0caf2070 to your computer and use it in GitHub Desktop.
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
extension UIView { | |
func embedView(_ view: UIView, inset: UIEdgeInsets = UIEdgeInsets.zero) { | |
let topConstraint = NSLayoutConstraint( | |
item: self, | |
attribute: .top, | |
relatedBy: .equal, | |
toItem: view, | |
attribute: .top, | |
multiplier: 1, | |
constant: inset.top | |
) | |
let leadingConstraint = NSLayoutConstraint( | |
item: self, | |
attribute: .leading, | |
relatedBy: .equal, | |
toItem: view, | |
attribute: .leading, | |
multiplier: 1, | |
constant: inset.left | |
) | |
let trailingConstraint = NSLayoutConstraint( | |
item: self, | |
attribute: .trailing, | |
relatedBy: .equal, | |
toItem: view, | |
attribute: .trailing, | |
multiplier: 1, | |
constant: inset.right | |
) | |
let bottomConstraint = NSLayoutConstraint( | |
item: self, | |
attribute: .bottom, | |
relatedBy: .equal, | |
toItem: view, | |
attribute: .bottom, | |
multiplier: 1, | |
constant: inset.bottom | |
) | |
view.translatesAutoresizingMaskIntoConstraints = false | |
addSubview(view) | |
addConstraints([leadingConstraint, trailingConstraint, topConstraint, bottomConstraint]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment