Skip to content

Instantly share code, notes, and snippets.

@congpc
Created June 25, 2016 13:17
// Creating constraints using NSLayoutConstraint: from iOS 6.0
NSLayoutConstraint(item: firstView, attribute: .Top, relatedBy: .Equal, toItem: secondView, attribute: .Bottom, multiplier: 1.0, constant: padding).active = true
NSLayoutConstraint(item: firstView, attribute: .CenterX, relatedBy: .Equal, toItem: secondView, attribute: .CenterX, multiplier: 1.0, constant: 0).active = true
NSLayoutConstraint(item: firstView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: equalHeight).active = true
// Creating constraints using Visual Format Language: from iOS 6.0
let views : [String : AnyObject] = ["centerXLabel": centerXLabel, "topEdgesLabel": topEdgesLabel]
var allConstraints = [NSLayoutConstraint]()
let constraint1 = NSLayoutConstraint.constraintsWithVisualFormat("V:[topEdgesLabel]-(<=130)-[centerXLabel(\(50))]",options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: views)
allConstraints += constraint1
NSLayoutConstraint.activateConstraints(allConstraints)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment