在我的表格 View 单元格中有 3 个对象; ImageView 、标签和 TextView 。
这就是我设置 textView 及其约束的方式:
textView.translatesAutoresizingMaskIntoConstraints = false
textView.textColor = UIColor.blue
textView.font = UIFont.systemFont(ofSize:15.0)
textView.textAlignment = NSTextAlignment.left
textView.isScrollEnabled = false
**注意,self
是一个包含textView 的UIView
。 TableView 单元格的内容 View 将包含此 UIView
self.addConstraints([
textView.topAnchor.constraint(equalTo: label.bottomAnchor, constant: 10.0),
textView.leftAnchor.constraint(equalTo: imageView.rightAnchor, constant: 10.0),
textView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -10.0),
textView.heightAnchor.constraint(greaterThanOrEqualToConstant: 20.0)
])
我设置了isScrollEnabled = false
,以便textView的高度根据其内容计算
对于我的 UITableView,我这样做了:
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
使用上面的代码,我的 tableviewcell 没有显示正确的高度。我相信这是因为我的 textview 高度约束给了 tableviewcell 内容 View 错误的高度。
我该如何解决这个问题?
最佳答案
- 设置
isScrollEnabled = false
不会影响textView高度的计算。 - 为了根据 textView 的内容调整单元格大小,除了 top(=) 之外,您还应该向
textView
添加 bottom(=) 约束> 和可选高度(>=)。
关于ios - Swift - 根据 UITextView 布局约束的 UITableViewCell 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40366937/