Skip to content

Instantly share code, notes, and snippets.

@fdstevex
Last active August 3, 2017 16:31
Show Gist options
  • Save fdstevex/af1c0a13a5e6ce696bf0 to your computer and use it in GitHub Desktop.
Save fdstevex/af1c0a13a5e6ce696bf0 to your computer and use it in GitHub Desktop.
Auto layout in playground
// Auto layout in a Swift Playground (for iOS).
import UIKit
var v = UIView()
v.frame = CGRectMake(0, 0, 200, 200)
var b1 = UIButton()
b1.setTitle("Click Me", forState:UIControlState.Normal)
b1.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
b1.setTranslatesAutoresizingMaskIntoConstraints(false)
v.addSubview(b1)
var b2 = UIButton()
b2.setTitle("Click Me Too", forState:UIControlState.Normal)
b2.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
b2.setTranslatesAutoresizingMaskIntoConstraints(false)
v.addSubview(b2)
var views = Dictionary<String, UIView>()
views["b1"] = b1
views["b2"] = b2
v.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[b1]-|", options: nil, metrics: nil, views: views))
v.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[b2]-|", options: nil, metrics: nil, views: views))
v.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[b1]-[b2]", options: nil, metrics: nil, views: views))
v.layoutIfNeeded()
b1.frame
b2.frame
v.frame
v
@fdstevex
Copy link
Author

fdstevex commented Jun 4, 2014

Needs to be an iOS project for this to work. Same method would work for AppKit, but class names would be different.

@jgis
Copy link

jgis commented Jun 12, 2014

You can set the Platform (iOS or OS X) in the file explorer: View -> Utilities -> Show File Explorer

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