Skip to content

Instantly share code, notes, and snippets.

@jtbandes
Last active February 4, 2021 15:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtbandes/6959f4f0b80e4cf7d0a0 to your computer and use it in GitHub Desktop.
Save jtbandes/6959f4f0b80e4cf7d0a0 to your computer and use it in GitHub Desktop.
// Would you rather use this:
NSLayoutConstraint(item: label, attribute: .Leading, relatedBy: .Equal, toItem: button, attribute: .Trailing, multiplier: 1, constant: 0).active = true
// or this?
label.constrain(.Leading, .Equal, to: button, .Trailing, plus: 20)
public extension NSView
{
func constrain(
attribute: NSLayoutAttribute,
_ relation: NSLayoutRelation,
to otherView: NSView,
_ otherAttribute: NSLayoutAttribute,
times multiplier: CGFloat = 1,
plus constant: CGFloat = 0
atPriority priority: NSLayoutPriority = 1000) -> NSLayoutConstraint
{
let c = NSLayoutConstraint(
item: self,
attribute: attribute,
relatedBy: relation,
toItem: otherView,
attribute: otherAttribute,
multiplier: multiplier,
constant: constant)
c.priority = priority
c.active = true
return c
}
}
@rinaldysam
Copy link

Awesomeeee

@jtbandes
Copy link
Author

jtbandes commented Feb 4, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment