title: create UITableViewCell constraint programmatically
body: <p>This is all taking place within this class:</p>
  
  <pre><code>class CustomCell2: UITableViewCell {
  </code></pre>
  
  <p>When I print the value of an existing width constraint (label = 0.7 * width of the cell) that was set in Storyboard, I see this:</p>
  
  <pre><code>&lt;NSLayoutConstraint:0x36b8750 UILabel:0x3d2aac0'Label'.width == 0.7*appName.CustomCell2:0x3e01880'CustomCell2'.width&gt;
  </code></pre>
  
  <p>When I try to create the same constraint programmatically, I get the error "The view hierarchy is not prepared for the constraint:"</p>
  
  <pre><code>&lt;NSLayoutConstraint:0xee08590 UILabel:0x3d2aac0'Label'.width == 0.9*appName.CustomCell2:0x3e01880'CustomCell2'.width&gt;
  </code></pre>
  
  <p>Seems exactly the same, so why can't that constraint be added?</p>
  
  <p>Constraint code in awakeFromNib() of CustomCell2:</p>
  
  <pre><code>let widthConstraint = NSLayoutConstraint(item: labelName, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.width, multiplier: 0.9, constant: 0)
  labelName.addConstraint(widthConstraint)
  </code></pre>
  
  <p>Rest of the error message:</p>
  
  <pre><code>When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug.
  2017-12-03 20:18:51.183 appName[899:684382] View hierarchy unprepared for constraint.
      Constraint: &lt;NSLayoutConstraint:0xee08590 UILabel:0x3d2aac0'Label'.width == 0.9*appName.CustomCell2:0x3e01880'CustomCell2'.width&gt;
      Container hierarchy: 
  &lt;UILabel: 0x3d2aac0; frame = (281 43; 674 54); text = 'Label'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = &lt;_UILabelLayer: 0x4233d30&gt;&gt;
      View not found in container hierarchy: &lt;appName.CustomCell2: 0x3e01880; baseClass = UITableViewCell; frame = (0 0; 963 160); layer = &lt;CALayer: 0xee08250&gt;&gt;
      That view's superview: NO SUPERVIEW
  Unable to install constraint on view.  Does the constraint reference something from outside the subtree of the view?  That's illegal. constraint:&lt;NSLayoutConstraint:0xee08590 UILabel:0x3d2aac0'Label'.width == 0.9*appName.CustomCell2:0x3e01880'CustomCell2'.width&gt; view:&lt;UILabel: 0x3d2aac0; frame = (281 43; 674 54); text = 'Label'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = &lt;_UILabelLayer: 0x4233d30&gt;&gt;
  </code></pre>
  
  <p>Thanks!</p>