Skip to content

Instantly share code, notes, and snippets.

@fdstevex
Last active August 3, 2017 16:31

Revisions

  1. fdstevex revised this gist Jun 4, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Auto layout in a Swift Playground.
    // Auto layout in a Swift Playground (for iOS).

    import UIKit

  2. fdstevex revised this gist Jun 4, 2014. No changes.
  3. fdstevex revised this gist Jun 3, 2014. 1 changed file with 4 additions and 6 deletions.
    10 changes: 4 additions & 6 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,9 @@
    // Trying to get auto layout to work in a Swift Playground.
    // This example should show a view with two buttons, one on top of the other.
    // But instead, the frames are all nil.
    // Auto layout in a Swift Playground.

    import UIKit

    var v = UIView()
    v.setTranslatesAutoresizingMaskIntoConstraints(false)
    v.frame = CGRectMake(0, 0, 200, 200)

    var b1 = UIButton()
    b1.setTitle("Click Me", forState:UIControlState.Normal)
    @@ -27,12 +25,12 @@ v.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[b1]-|", op

    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.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[b1]-[b2]", options: nil, metrics: nil, views: views))

    v.layoutIfNeeded()

    b1.frame
    b2.frame
    v.frame

    v
    v
  4. fdstevex revised this gist Jun 3, 2014. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Trying to get auto layout to work in a Swift Playground.
    // Trying to get auto layout to work in a Swift Playground.
    // This example should show a view with two buttons, one on top of the other.
    // But instead, the frames are all nil.

    @@ -29,8 +29,10 @@ v.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[b2]-|", op

    v.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[b1]-[b2]-|", options: nil, metrics: nil, views: views))

    v.layoutIfNeeded()

    b1.frame
    b2.frame
    v.frame

    v
    v
  5. fdstevex revised this gist Jun 3, 2014. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    // Playground - noun: a place where people can play
    // Trying to get auto layout to work in a Swift Playground.
    // This example should show a view with two buttons, one on top of the other.
    // But instead, the frames are all nil.

    import UIKit

    @@ -31,3 +33,4 @@ b1.frame
    b2.frame
    v.frame

    v
  6. fdstevex created this gist Jun 3, 2014.
    33 changes: 33 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    // Playground - noun: a place where people can play

    import UIKit

    var v = UIView()
    v.setTranslatesAutoresizingMaskIntoConstraints(false)

    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))

    b1.frame
    b2.frame
    v.frame