我创建了一个带有自定义视图的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
的{{1}}中,我添加了视图:
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
或将常量设置为负值,但不起作用。
答案 0 :(得分:0)
当您将xib视图添加为子视图时,它的超级视图没有任何约束。您应该以编程方式创建它们。 https://docs.google.com/viewer?url=https://www.unipg.it/files/pagine/410/4-PDF-A.pdf一个很好的例子。例如,如果您想将视图放在超视图的中心并且大小相同,则可以写下:
{{1}}
它看起来像这样(蓝色 - tableView,红色 - myView):
答案 1 :(得分:0)
最后我发现了这个问题:
问题在于xibSetup()
方法,因为我加载了xib,但是我没有设置约束!所以我的新方法是:
func xibSetup() {
let myView = loadViewFromNib()
myView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(myView)
let leading = NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: myView.superview, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0)
self.addConstraint(leading)
let bottom = NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: myView.superview, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)
self.addConstraint(bottom)
let trailing = NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: myView.superview, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0)
self.addConstraint(trailing)
let top = NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: myView.superview, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
self.addConstraint(top)
}
这解决了我的问题。