我在Prototype单元格中的IB中创建了一个UIView并添加了约束。单元格有一个自定义类。当我尝试在cellForRowAtIndexPath中获取UIView的宽度时,它返回476而不是251.在阅读了关于这个主题的SO和许多答案之后,我试图在cellForRowAtIndexPath或子类中调用setNeedsLayout和layoutSubviews()但是都没有返回正确的价值。高度20是正确的。任何帮助非常感谢!
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("EventCell") as! CustomResultsTVCell
// cell.setNeedsLayout()
let maxWidth = cell.barView.frame.width
print("barview maxWidth is \(maxWidth)")
// prints 476.0
答案 0 :(得分:0)
您添加了trailing
和leading
约束55和10.这会使宽度成为您的父视图,在这种情况下CustomResultsTVCell
减去{{1}的55 {和} trailing
空间中的10个。
如果要获得硬编码的251宽度,则应将leading
设置为等于251 Width
。
或者如果你真的认为在应用constraint
和trailing
空格后你想要获得251宽度,那么我认为你leading
或CustomResultsTVCell
的约束不是你想的宽度。也许向我们展示这些的约束。