【发布时间】:2017-06-16 00:24:23
【问题描述】:
我正在尝试创建一个包含单元格的表格视图,每个单元格都包含一个文本视图,该视图应该占据整个单元格并调整单元格的高度以适合其内容。水平和垂直内容拥抱和抗压优先级是相应设置的。
我已将单元格设置为自动高度
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
在我的cellForRowAt... 中,我为文本视图的顶部、前导、尾部和底部边缘创建约束,将其约束在单元格的内容视图中。
问题是在运行时文本视图不会占用多行并增加单元格的大小。相反,它会从单元的后缘流出。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.translatesAutoresizingMaskIntoConstraints = false
cell.contentView.translatesAutoresizingMaskIntoConstraints = false
let entry = server.log[indexPath.row]
let textView = UITextView()
textView.isScrollEnabled = false
textView.isEditable = false
//textView.isUserInteractionEnabled = true
textView.text = entry.message
textView.translatesAutoresizingMaskIntoConstraints = false
cell.contentView.addSubview(textView)
let top = NSLayoutConstraint(item: textView, attribute: .top, relatedBy: .equal, toItem: cell.contentView, attribute: .topMargin, multiplier: 1.0, constant: 0)
let leading = NSLayoutConstraint(item: textView, attribute: .leading, relatedBy: .equal, toItem: cell.contentView, attribute: .leadingMargin, multiplier: 1.0, constant: 0)
let trailing = NSLayoutConstraint(item: textView, attribute: .trailing, relatedBy: .equal, toItem: cell.contentView, attribute: .trailingMargin, multiplier: 1.0, constant: 0)
let bottom = NSLayoutConstraint(item: textView, attribute: .bottom, relatedBy: .equal, toItem: cell.contentView, attribute: .bottomMargin, multiplier: 1.0, constant: 0)
//top.priority = 999
//leading.priority = 999
trailing.priority = 999
bottom.priority = 999
cell.contentView.addConstraints([top, bottom, leading, trailing])
textView.setContentCompressionResistancePriority(1000, for: .horizontal)
textView.setContentCompressionResistancePriority(1000, for: .vertical)
textView.setContentHuggingPriority(1001, for: .horizontal)
textView.setContentHuggingPriority(1001, for: .vertical)
return cell;
}
单元格在运行时的显示方式
框架的显示方式
视图层次调试器报告的内容
【问题讨论】:
标签: ios swift autolayout