Using auto layout at the expense of having no frame <p>I have a box that is initiated without a frame so it has 0 center and size.</p> <pre><code>let blackBox = UIView() blackBox.backgroundColor = .blackColor() blackBox.translatesAutoresizingMaskIntoConstraints = false view.addSubview(blackBox) view.addConstraint(NSLayoutConstraint(item: blackBox, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: 0.1, constant: 0)) view.addConstraint(NSLayoutConstraint(item: blackBox, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Height, multiplier: 0.5, constant: 0)) view.addConstraint(NSLayoutConstraint(item: blackBox, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1, constant: 0)) view.addConstraint(NSLayoutConstraint(item: blackBox, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0)) </code></pre> <p>Is there anyway to insert a centered subview into the <code>blackBox</code> <strong>without</strong> using autolayout or is there a <strong>neater</strong> way I could/should use it. The only way I can see this happening is:</p> <pre><code>let blueBox = UIView() blackBox.addSubview(blueBox) blueBox.translatesAutoresizingMaskIntoConstraints = false blueBox.backgroundColor = UIColor.blueColor() view.addConstraint(NSLayoutConstraint(item: blueBox, attribute: .CenterX, relatedBy: .Equal, toItem: blackBox, attribute: .CenterX, multiplier: 1, constant: 0)) view.addConstraint(NSLayoutConstraint(item: blueBox, attribute: .CenterY, relatedBy: .Equal, toItem: blackBox, attribute: .CenterY, multiplier: 1, constant: 0)) view.addConstraint(NSLayoutConstraint(item: blueBox, attribute: .Width, relatedBy: .Equal, toItem: blackBox, attribute: .Width, multiplier: 0.4, constant: 0)) view.addConstraint(NSLayoutConstraint(item: blueBox, attribute: .Height, relatedBy: .Equal, toItem: blackBox, attribute: .Height, multiplier: 0.4, constant: 0)) </code></pre>