Auto Layout

Two different ways to use auto layout to set a view full width and height in a parent view.

translatesAutoresizingMaskIntoConstraints = false
let views = ["messageView": self, "parent": parentView]
 
parentView.addConstraints(
	NSLayoutConstraint.constraintsWithVisualFormat("H:|[messageView]|",
	options: [],
	metrics: nil,
	views: views
))
 
parentView.addConstraints(
	NSLayoutConstraint.constraintsWithVisualFormat("V:|-64-[messageView]",
	options: [],
	metrics: nil,
	views: views
))
 
parentView.layoutIfNeeded()

Notes

"H:|[messageView]|" H = horizontal | = parent view's edge [] = view, contained with name from array passed in "V:|-64-[messageView]" V = vertical -64- = 64 from the top of the view in this case. Can also mix in '-(<=0)-' to set greater or less than "V:[messageView(100)]" () = set the height of element

NSLayoutConstraint()

To do what we did above in but with an array of NSLayoutConstraints

var constraints = [NSLayoutConstraint]()
let topEdge = NSLayoutConstraint(
	item: self,
	attribute: NSLayoutAttribute.Top,
	relatedBy: NSLayoutRelation.Equal,
	toItem: parentView,
	attribute: NSLayoutAttribute.Top,
	multiplier: 1,
	constant: 64
)
 
constraints.append(topEdge)
let leadingEdge = NSLayoutConstraint(
	item: self,
	attribute: NSLayoutAttribute.Leading,
	relatedBy: NSLayoutRelation.Equal,
	toItem: parentView,
	attribute: NSLayoutAttribute.Leading,
	multiplier: 1,
	constant: 0
)
 
constraints.append(leadingEdge)
let trailingEdge = NSLayoutConstraint(
	item: self,
	attribute: NSLayoutAttribute.Trailing,
	relatedBy: NSLayoutRelation.Equal,
	toItem: parentView,
	attribute: NSLayoutAttribute.Trailing,
	multiplier: 1,
	constant: 0
)
 
constraints.append(trailingEdge)
parentView.addConstraints(constraints)