title: Add a view from a xib file to a UITableViewCell adding constraints
body: <p>I created a xib file with a custom view that I want to display in a <code>UITableViewCell</code>.</p>
  
  <p>The problem is: in the xib file, I set the width of the <code>UIView</code> to 600 (the <code>size</code> is <code>Freeform</code>) but I want to use constraints in order to obtain the correct width for every device.</p>
  
  <p>I add this view in a <code>UITableViewCell</code> with <code>addSubview()</code> so I think that this view must be constraint to the view of the cell, is that right? How can I do this?</p>
  
  <p>I add some code:</p>
  
  <p>This my custom <code>UIView</code> related to the xib file:</p>
  
  <pre><code>class DownloadView: UIView, NSURLSessionDownloadDelegate{
  
  ///utilizzata per mostrare il nome del file in download
  @IBOutlet var nomeFileInDownloadLabel: UILabel!
  
  ///utilizzata per mostrare la percentuale di download completato
  @IBOutlet var quantitaScaricataLabel: UILabel!
  
  ///utilizzata per mostrare il download
  @IBOutlet var progressView: UIProgressView!
  
  var view : UIView!
  
  private var downloadTask: NSURLSessionDownloadTask?
  
  //Initialization
  override init(frame: CGRect) {
      super.init(frame: frame)
      xibSetup()
  }
  
  required init(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)!
  }
  
  
  //I setup the xib from here
  func xibSetup() {
      view = loadViewFromNib()
  
      //if I set the following one the view is a square
      //view.frame = bounds
  
      // Make the view stretch with containing view
      view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
      self.addSubview(view)
  }
  
  //I load the xib file
  func loadViewFromNib() -&gt; UIView {
  
      let bundle = NSBundle(forClass: self.dynamicType)
      let nib = UINib(nibName: "View", bundle: bundle)
      let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
      return view
  }
  //some code for other task 
  ...
  }
  </code></pre>
  
  <p>In my <code>UITableView</code> in the method <code>tableView(tableView:cellForRowAtIndexPath)</code> I add the view:</p>
  
  <pre><code>override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCellWithIdentifier("DownloadRootTVC_IDcell", forIndexPath: indexPath) as! DownloadTVCell
  
      //I get my custom view from an array
      let myView = self.listOfViewsDownlaod[indexPath.row]
  
      //I add the view to the cell
      cell.addSubview(myView)
  
  
      return cell
  }
  </code></pre>
  
  <p>In this last method I think that I have to setup the constraint in order to fit my custom view in the <code>UITableViewCell</code> adapting it to the device width.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Since I want to adapt the width and the height of the subview to the cell I wrote this:</p>
  
  <pre><code>    let topConstraint = NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: cell, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 8)
      cell.addConstraint(topConstraint)
  
      let bottomConstraint = NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: cell, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 8)
      cell.addConstraint(bottomConstraint)
  
      let leftConstraint = NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: cell, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 20)
      cell.addConstraint(leftConstraint)
  
      let rightConstraint = NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.TrailingMargin, relatedBy: NSLayoutRelation.Equal, toItem: cell, attribute: NSLayoutAttribute.TrailingMargin, multiplier: 1, constant: 20)
      cell.addConstraint(rightConstraint)
  </code></pre>
  
  <p>The only one that doesn't work is the right one, the view continue over the screen. I also tried to use <code>NSLayoutAttribute.Right</code> or <code>NSLayoutAttribute.RighMargin</code> or to set the constant to a negative value, but doesn't work.</p>