Skip to content

Instantly share code, notes, and snippets.

@preble
Created November 7, 2014 16:22
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 preble/b819c22f6de4d68a5606 to your computer and use it in GitHub Desktop.
Save preble/b819c22f6de4d68a5606 to your computer and use it in GitHub Desktop.
NSView extension: insetSubview(_:dx:dy:)
extension NSView {
func insetSubview(subview: NSView, dx: CGFloat, dy: CGFloat) {
addSubview(subview)
subview.translatesAutoresizingMaskIntoConstraints = false
let constrain = { (attribute: NSLayoutAttribute, constant: CGFloat) in
subview.addConstraint(NSLayoutConstraint(
item: subview,
attribute: attribute,
relatedBy: .Equal,
toItem: self,
attribute: attribute,
multiplier: 1.0,
constant: constant))
}
constrain(.Left, dx)
constrain(.Right, dx)
constrain(.Top, dy)
constrain(.Bottom, dy)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment