I created a xib file with a custom view that I want to display in a UITableViewCell.
The problem is: in the xib file, I set the width of the UIView to 600 (the size is Freeform) but I want to use constraints in order to obtain the correct width for every device.
I add this view in a UITableViewCell with addSubview() so I think that this view must be constraint to the view of the cell, is that right? How can I do this?
I add some code:
This my custom UIView related to the xib file:
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() -> 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 
...
}
In my UITableView in the method tableView(tableView:cellForRowAtIndexPath) I add the view:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> 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
}
In this last method I think that I have to setup the constraint in order to fit my custom view in the UITableViewCell adapting it to the device width.
EDIT:
Since I want to adapt the width and the height of the subview to the cell I wrote this:
    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)
The only one that doesn't work is the right one, the view continue over the screen. I also tried to use NSLayoutAttribute.Right or NSLayoutAttribute.RighMargin or to set the constant to a negative value, but doesn't work.
 
    
 
    