Skip to content

Instantly share code, notes, and snippets.

@fdln
Forked from fdstevex/gist:af1c0a13a5e6ce696bf0
Last active August 29, 2015 14:08
Show Gist options
  • Save fdln/997bf62351802288200d to your computer and use it in GitHub Desktop.
Save fdln/997bf62351802288200d to your computer and use it in GitHub Desktop.
// 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment