Skip to content

Instantly share code, notes, and snippets.

@rubenroques
Last active August 29, 2015 14:14
Show Gist options
  • Save rubenroques/5c201cd571cd5913f688 to your computer and use it in GitHub Desktop.
Save rubenroques/5c201cd571cd5913f688 to your computer and use it in GitHub Desktop.
Playground - Testing NSLayoutConstraints
// Playground - noun: a place where people can play
import UIKit
var base = UIView()
base.frame = CGRectMake(0, 0, 400, 500)
base.backgroundColor = UIColor.lightGrayColor()
var topImageView = UIImageView()
topImageView.setTranslatesAutoresizingMaskIntoConstraints(false)
topImageView.backgroundColor = UIColor.greenColor()
var labelA = UILabel()
labelA.setTranslatesAutoresizingMaskIntoConstraints(false)
labelA.backgroundColor = UIColor.grayColor()
labelA.text = "Label A"
var labelB = UILabel()
labelB.setTranslatesAutoresizingMaskIntoConstraints(false)
labelB.backgroundColor = UIColor.grayColor()
labelB.text = "Label B"
var labelC = UILabel()
labelC.setTranslatesAutoresizingMaskIntoConstraints(false)
labelC.backgroundColor = UIColor.grayColor()
labelC.text = "Label C"
var views = Dictionary<String, UIView>()
views["image"] = topImageView
views["labelA"] = labelA
views["labelB"] = labelB
views["labelC"] = labelC
base.addSubview(topImageView)
base.addSubview(labelA)
base.addSubview(labelB)
base.addSubview(labelC)
//Main constraints
base.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[image]-20-|", options: nil, metrics: nil, views: views))
base.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-40-[image(80)]", options: nil, metrics: nil, views: views))
base.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[labelA]-20-|", options: nil, metrics: nil, views: views))
base.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[labelB]-20-|", options: nil, metrics: nil, views: views))
base.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[labelC]-20-|", options: nil, metrics: nil, views: views))
base.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[image]-20-[labelA]", options: nil, metrics: nil, views: views))
base.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[labelA]-20-[labelB]", options: nil, metrics: nil, views: views))
base.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[labelB]-20-[labelC]", options: nil, metrics: nil, views: views))
//Fallback constraints
var constraintLabelCTop2 = NSLayoutConstraint(item: labelC, attribute: .Top, relatedBy: .Equal, toItem: labelA, attribute: .Bottom, multiplier: 1, constant: 20)
constraintLabelCTop2.priority = 100
var constraintLabelCTop3 = NSLayoutConstraint(item: labelC, attribute: .Top, relatedBy: .Equal, toItem: topImageView, attribute: .Bottom, multiplier: 1, constant: 20)
constraintLabelCTop3.priority = 100
base.addConstraint(constraintLabelCTop2)
base.addConstraint(constraintLabelCTop3)
var constraintLabelBTop2 = NSLayoutConstraint(item: labelB, attribute: .Top, relatedBy: .Equal, toItem: topImageView, attribute: .Bottom, multiplier: 1, constant: 20)
constraintLabelBTop2.priority = 100
base.addConstraint(constraintLabelBTop2)
//=============
//Tests
//labelA.removeFromSuperview() //Uncomment
//labelB.removeFromSuperview() //Uncomment
//labelC.removeFromSuperview() //Uncomment
base.layoutIfNeeded()
base // Add this to timeline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment