Skip to content

Instantly share code, notes, and snippets.

@wh1pch81n
Last active December 28, 2016 21:29
Show Gist options
  • Save wh1pch81n/076afb31e30e4fd019a8920f2ac2a8b4 to your computer and use it in GitHub Desktop.
Save wh1pch81n/076afb31e30e4fd019a8920f2ac2a8b4 to your computer and use it in GitHub Desktop.
// NSLayoutConstraint
// Given an array of 3 views
let viewArray = [
greenView_vf,
redView_vf,
blueView_vf
]
// set this value to false to make auto layout work
viewArray.forEach({ $0.translatesAutoresizingMaskIntoConstraints = false })
// add the 3 views to the super view
viewArray.forEach(view_vf.addSubview)
let viewDict = [
"greenView" : greenView_vf,
"redView" : redView_vf,
"blueView" : blueView_vf
]
//1
view_vf.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-[greenView]-15.5-[redView]-|"
, options: NSLayoutFormatOptions(rawValue: 0)
, metrics: nil, views: viewDict))
//2
view_vf.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-[blueView]-|"
, options: NSLayoutFormatOptions(rawValue: 0)
, metrics: nil, views: viewDict))
//3
view_vf.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-[greenView]-[blueView]-|"
, options: NSLayoutFormatOptions(rawValue: 0)
, metrics: nil, views: viewDict))
//4
view_vf.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-[redView]-[blueView]-|"
, options: NSLayoutFormatOptions(rawValue: 0)
, metrics: nil, views: viewDict))
//5
greenView_vf.widthAnchor.constraint(equalTo: redView_vf.widthAnchor).isActive = true
//6
greenView_vf.heightAnchor.constraint(equalTo: blueView_vf.heightAnchor).isActive = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment