title: iOS autolayout view centered in parent, with margins and dynamic height
body: <p>I'm creating a popup, using auto layout. My popup have a dynamic height, and must be centered in the window, with margins. It has a fixed width.</p>
  
  <p>I added the following constraints:</p>
  
  <ul>
  <li>width = fixed value</li>
  <li>height less than or equal to a computed value</li>
  <li>top margin = container top margin + fixed value</li>
  <li>bottom margin = container bottom margin + fixed value</li>
  <li>center x = container center x</li>
  <li>center y = container center y</li>
  </ul>
  
  <p>It seems to work, but Xcode shows a warning:</p>
  
  <blockquote>
    <p>Unable to simultaneously satisfy constraints.</p>
    
    <p>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) </p>
    
    <p>(</p>
  
  <pre><code>"&lt;NSLayoutConstraint:0x7e572a40 V:|-(&gt;=19)-[UIView:0x7a8bb6a0]   (Names: '|':UIView:0x7e572140 )&gt;",
  "&lt;NSLayoutConstraint:0x7e573620 UIView:0x7a8bb6a0.bottom &gt;= UIView:0x7e572140.bottom + 19&gt;",
  "&lt;NSLayoutConstraint:0x7e573950 UIView:0x7a8bb6a0.centerY == UIView:0x7e572140.centerY&gt;"
  </code></pre>
    
    <p>)</p>
    
    <p>Will attempt to recover by breaking constraint </p>
  
  <pre><code>&lt;NSLayoutConstraint:0x7e573620 UIView:0x7a8bb6a0.bottom &gt;= UIView:0x7e572140.bottom + 19&gt;
  </code></pre>
  </blockquote>
  
  <p>What's wrong? If I remove the constraint that Xcode breaks, it does not work anymore (my popup is no longer visible).</p>
  
  <p>Here is my constraints code:</p>
  
  <pre><code>self.view.addConstraint(NSLayoutConstraint(item:_mainContainer,
                                             attribute:NSLayoutAttribute.width,
                                             relatedBy:NSLayoutRelation.equal,
                                             toItem:nil,
                                             attribute:NSLayoutAttribute.notAnAttribute,
                                             multiplier: 1,
                                             constant: UIPopup.defaultSize.width));
  
  self.view.addConstraint(NSLayoutConstraint(item:_mainContainer,
                                             attribute:NSLayoutAttribute.height,
                                             relatedBy:NSLayoutRelation.lessThanOrEqual,
                                             toItem:nil,
                                             attribute:NSLayoutAttribute.notAnAttribute,
                                             multiplier: 1,
                                             constant:popupHeight));
  
  self.view.addConstraint(NSLayoutConstraint(item:_mainContainer,
                                             attribute:NSLayoutAttribute.top,
                                             relatedBy:NSLayoutRelation.greaterThanOrEqual,
                                             toItem:self.view,
                                             attribute:NSLayoutAttribute.top,
                                             multiplier: 1,
                                             constant:verticalMargin));
  
  
  self.view.addConstraint(NSLayoutConstraint(item:_mainContainer,
                                             attribute:NSLayoutAttribute.bottom,
                                             relatedBy:NSLayoutRelation.greaterThanOrEqual,
                                             toItem:self.view,
                                             attribute:NSLayoutAttribute.bottom,
                                             multiplier: 1,
                                             constant:verticalMargin));
  
  self.view.addConstraint(NSLayoutConstraint(item:_mainContainer,
                                             attribute:NSLayoutAttribute.centerX,
                                             relatedBy:NSLayoutRelation.equal,
                                             toItem:self.view,
                                             attribute:NSLayoutAttribute.centerX,
                                             multiplier: 1,
                                             constant: 0));
  
  self.view.addConstraint(NSLayoutConstraint(item:_mainContainer,
                                             attribute:NSLayoutAttribute.centerY,
                                             relatedBy:NSLayoutRelation.equal,
                                             toItem:self.view,
                                             attribute:NSLayoutAttribute.centerY,
                                             multiplier: 1,
                                             constant: 0));
  </code></pre>