【发布时间】:2015-12-20 09:44:57
【问题描述】:
我创建了一个带有自定义视图的 xib 文件,我想在 UITableViewCell 中显示该文件。
问题是:在 xib 文件中,我将 UIView 的宽度设置为 600(size 是 Freeform)但我想使用约束来获得每个设备的正确宽度。
我在UITableViewCell 和addSubview() 中添加了这个视图,所以我认为这个视图必须约束到单元格的视图,对吗?我该怎么做?
我添加了一些代码:
这是我自定义的UIView 与 xib 文件相关:
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
...
}
在我的UITableView 方法tableView(tableView:cellForRowAtIndexPath) 中添加视图:
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
}
在最后一种方法中,我认为我必须设置约束以适应 UITableViewCell 中的自定义视图,使其适应设备宽度。
编辑:
由于我想使子视图的宽度和高度适应单元格,所以我写了这个:
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)
唯一不起作用的是正确的,视图继续在屏幕上。我也尝试使用NSLayoutAttribute.Right 或NSLayoutAttribute.RighMargin 或将常量设置为负值,但不起作用。
【问题讨论】:
标签: swift uitableview constraints xib