Skip to content

Instantly share code, notes, and snippets.

@kazukitanaka0611
Created February 6, 2015 03:49
Show Gist options
  • Save kazukitanaka0611/5903008522e69f38af48 to your computer and use it in GitHub Desktop.
Save kazukitanaka0611/5903008522e69f38af48 to your computer and use it in GitHub Desktop.
AutoLayout code view in view
let myView1 = UIView()
myView1.backgroundColor = UIColor.greenColor()
myView1.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(myView1)
let myView2 = UIView()
myView2.backgroundColor = UIColor.orangeColor()
myView2.setTranslatesAutoresizingMaskIntoConstraints(false)
myView1.addSubview(myView2)
myView1.addConstraints([
NSLayoutConstraint(
item: myView2,
attribute: .Top,
relatedBy: .Equal,
toItem: myView1,
attribute: .Top,
multiplier: 1,
constant: 70),
NSLayoutConstraint(
item: myView2,
attribute: .Bottom,
relatedBy: .Equal,
toItem: myView1,
attribute: .Bottom,
multiplier: 1,
constant: -70),
NSLayoutConstraint(
item: myView2,
attribute: .Right,
relatedBy: .Equal,
toItem: myView1,
attribute: .Right,
multiplier: 1,
constant: -70),
NSLayoutConstraint(
item: myView2,
attribute: .Left,
relatedBy: .Equal,
toItem: myView1,
attribute: .Left,
multiplier: 1,
constant: 70)
])
self.view.addConstraints([
NSLayoutConstraint(
item: myView1,
attribute: .Top,
relatedBy: .Equal,
toItem: self.view,
attribute: .Top,
multiplier: 1,
constant: 50),
NSLayoutConstraint(
item: myView1,
attribute: .Bottom,
relatedBy: .Equal,
toItem: self.view,
attribute: .Bottom,
multiplier: 1,
constant: -50),
NSLayoutConstraint(
item: myView1,
attribute: .Right,
relatedBy: .Equal,
toItem: self.view,
attribute: .Right,
multiplier: 1,
constant: -50),
NSLayoutConstraint(
item: myView1,
attribute: .Left,
relatedBy: .Equal,
toItem: self.view,
attribute: .Left,
multiplier: 1,
constant: 50)
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment