Skip to content

Instantly share code, notes, and snippets.

@NikhilManapure
Created September 10, 2020 09:24
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 NikhilManapure/1eeb64205a0df0dcbb45705c0caf2070 to your computer and use it in GitHub Desktop.
Save NikhilManapure/1eeb64205a0df0dcbb45705c0caf2070 to your computer and use it in GitHub Desktop.
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