vil*_*ssu 21 uitableview ios autolayout
我完全在代码中创建我的UI,并使用Masonry将单元格的内容视图的子视图约束到适当的高度.我正在使用
[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height
Run Code Online (Sandbox Code Playgroud)
在iOS 7上的行高,而iOS 8自动处理它.
所有东西看起来都应该在屏幕上显示,但是在控制台中我得到了大量的警告,用于冲突的约束,这些都似乎是由于单元内容视图上的未经处理和不必要的高度约束(例如<NSLayoutConstraint UITableViewCellContentView.height == 44>).
在iOS 8上,我将表视图设置rowHeight为UITableViewAutomaticDimension(有效-1),但我仍然得到这个约束.我只在内容视图和它自己的子视图之间添加约束,因此内容视图和单元格本身之间没有约束.
知道这个约束来自哪里以及如何让它消失?
编辑:现在我实际上找到了各种各样的"解决方案" - 最初将内容视图的框架设置为荒谬的东西,比如CGRectMake(0, 0, 99999, 99999),在添加子视图或约束之前,似乎会使警告消失.但这并不是正确的方式,所以有人能说出更好的方法吗?
Mar*_*cel 22
我有同样的问题,并修复它设置单元格的自动调整大小掩码,如下所示:
override func awakeFromNib() {
super.awakeFromNib()
self.contentView.autoresizingMask = .flexibleHeight
}
Run Code Online (Sandbox Code Playgroud)
同样在控制器中我设置了估计的高度并告诉表视图使用自动维度(在viewDidLoad方法中:
self.tableView.estimatedRowHeight = 120
self.tableView.rowHeight = UITableView.automaticDimension
Run Code Online (Sandbox Code Playgroud)
这些链接有助于:
http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html
UITableViewCell中iOS7上的自动布局约束问题
希望这可以帮助!
为了达到接受的答案 - 在试图让iOS 8的自动细胞大小工作几个月后,我发现了一个重要的警告.必须设置'estimatedRowHeight'属性.通过tableView直接或通过实现委托方法.即使无法确定有效估计值,只需提供默认值(0.0)以外的值就可以证明iOS 8的单元格布局在我的测试中有效.
关于问题编辑中提到的“解决方案”(将contentView框架临时设置为大的东西),这里证明这是一个很好的“解决方案”:https : //github.com/smileyborg/TableViewCellWithAutoLayoutiOS8/blob/master/ TableViewCellWithAutoLayout / TableViewController / TableViewCell.swift
// Note: if the constraints you add below require a larger cell size than the current size (which is likely to be the default size {320, 44}), you'll get an exception.
// As a fix, you can temporarily increase the size of the cell's contentView so that this does not occur using code similar to the line below.
// See here for further discussion: https://github.com/Alex311/TableCellWithAutoLayout/commit/bde387b27e33605eeac3465475d2f2ff9775f163#commitcomment-4633188
// contentView.bounds = CGRect(x: 0.0, y: 0.0, width: 99999.0, height: 99999.0)
Run Code Online (Sandbox Code Playgroud)
它很hacky,但似乎可以正常工作。