title: How to add constraints programmatically using Swift
body: <p>I'm trying to figure this out since last week without going any step further. Ok, so I need to apply some <strong>constraints</strong> <strong><em>programmatically</em></strong> in <strong>Swift</strong> to a <code>UIView</code> using this code:</p>
  
  <pre><code>var new_view:UIView! = UIView(frame: CGRectMake(0, 0, 100, 100));
  new_view.backgroundColor = UIColor.redColor();
  view.addSubview(new_view);
  
  var constX:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0);
  self.view.addConstraint(constX);
  
  var constY:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0);
  self.view.addConstraint(constY);
  
  var constW:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: new_view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0);
  self.view.addConstraint(constW);
  
  var constH:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: new_view, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0);
  self.view.addConstraint(constH);
  </code></pre>
  
  <p>But Xcode returns this weird output:</p>
  
  <pre><code>2014-10-03 09:48:12.657 Test[35088:2454916] Unable to simultaneously satisfy constraints.  Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
  (
  "&lt;NSLayoutConstraint:0x7fa4ea446830 UIView:0x7fa4ea429290.centerX == UIView:0x7fa4ea4470f0.centerX&gt;",
  "&lt;NSAutoresizingMaskLayoutConstraint:0x7fa4ea4516c0 h=--&amp; v=--&amp; UIView:0x7fa4ea429290.midX == + 50&gt;",
  "&lt;NSLayoutConstraint:0x7fa4ea452830 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fa4ea4470f0(375)]&gt;",
  "&lt;NSAutoresizingMaskLayoutConstraint:0x7fa4ea446db0 h=-&amp;- v=-&amp;- 'UIView-Encapsulated-Layout-Left' H:|-(0)-[UIView:0x7fa4ea4470f0]   (Names: '|':UIWindow:0x7fa4ea444b20 )&gt;"
  )
  
  Will attempt to recover by breaking constraint &lt;NSLayoutConstraint:0x7fa4ea446830 UIView:0x7fa4ea429290.centerX == UIView:0x7fa4ea4470f0.centerX&gt;
  
  Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in
  &lt;UIKit/UIView.h&gt; may also be helpful.
  
  2014-10-03 09:48:12.658 Test[35088:2454916] Unable to simultaneously satisfy constraints.  Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
  (
  "&lt;NSLayoutConstraint:0x7fa4ea44d160 UIView:0x7fa4ea429290.centerY == UIView:0x7fa4ea4470f0.centerY&gt;",
  "&lt;NSAutoresizingMaskLayoutConstraint:0x7fa4ea451b30 h=--&amp; v=--&amp; UIView:0x7fa4ea429290.midY == + 50&gt;",
  "&lt;NSLayoutConstraint:0x7fa4ea44cf00 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7fa4ea4470f0(667)]&gt;",
  "&lt;NSAutoresizingMaskLayoutConstraint:0x7fa4ea452700 h=-&amp;- v=-&amp;- 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UIView:0x7fa4ea4470f0]  (Names: '|':UIWindow:0x7fa4ea444b20 )&gt;"
  )
  
  Will attempt to recover by breaking constraint &lt;NSLayoutConstraint:0x7fa4ea44d160 UIView:0x7fa4ea429290.centerY == UIView:0x7fa4ea4470f0.centerY&gt;
  
  Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in &lt;UIKit/UIView.h&gt; may also be helpful.
  </code></pre>
  
  <p>Can you help me?
  Thanks a lot</p>