Implementing iOS 8 Auto Layout Constraints in Swift Code – Techotopia

my review point is 10/10

NSLayoutConstraint 사용법의 예시 (길이10분정도)

//  ViewController.swift

//  boundsAndFrame

//

//  Created by AJ Norton on 8/27/15.

//  Copyright © 2015 AJ Norton. All rights reserved.

//

import UIKit

class ViewController: UIViewController {

 override func viewDidLoad() {

   super.viewDidLoad()

   // Do any additional setup after loading the view, typically from a nib.

   let v1 = UIView(frame: CGRectMake(50, 50, 300, 200))

   v1.backgroundColor = UIColor.redColor()

   self.view.addSubview(v1)

   let v2 = UIView()

   v2.backgroundColor = UIColor.greenColor()

   v2.setTranslatesAutoresizingMaskIntoConstraints(false)

   v1.addSubview(v2)

   v1.addConstraint(

     NSLayoutConstraint(item: v2,

       attribute: NSLayoutAttribute.Left,

     relatedBy: NSLayoutRelation.Equal,

     toItem: v1,

     attribute: .Left,

     multiplier: 1,

     constant: 0))

   v1.addConstraint(

     NSLayoutConstraint(item: v2,

       attribute: NSLayoutAttribute.Right,

       relatedBy: NSLayoutRelation.Equal,

       toItem: v1,

       attribute: .Right,

       multiplier: 1,

       constant: 0))

   v1.addConstraint(

     NSLayoutConstraint(item: v2,

       attribute: NSLayoutAttribute.Top,

       relatedBy: NSLayoutRelation.Equal,

       toItem: v1,

       attribute: .Top,

       multiplier: 1,

       constant: 0))

   v1.addConstraint(

     NSLayoutConstraint(item: v2,

       attribute: NSLayoutAttribute.Height,

       relatedBy: NSLayoutRelation.Equal,

       toItem: v1,

       attribute: .Height,

       multiplier: 0.5,

       constant: 0))

 }

 override func didReceiveMemoryWarning() {

   super.didReceiveMemoryWarning()

   // Dispose of any resources that can be recreated.

 }

}

Swift NSLayoutConstraint programatically sample code – Knowledge Stack

Goodbye Spacer Views Hello Layout Guides

Size Class Concepts in iOS