问题描述
我在项目中使用自动调整单元格的大小.我想将 cells 保持在最小height
,但是我不能为 cells 中的 cells 设置最小的height
constraint . em>内容视图(在添加新约束视图中为灰色:
I am using automatically resizing cells in my project. I want to keep the cells at a minimum height
, but I cannot set a minimum height
constraint for my cells in my Content View (as it is greyed out in the Add New Constraints view:
反正有为我的单元格添加最小height
约束的方法吗?
Is there anyway to add minimum height
constraints for my cells?
推荐答案
启用UITableViewAutomaticDimension
后,系统会调用UITableViewCell
的 systemLayoutSizeFittingSize:withHorizontalFittingPriority:verticalFittingPriority:方法来计算像元高度,因此您可以将UITableViewCell
子类化并覆盖此方法以为像元强制最小高度这样
When UITableViewAutomaticDimension
is enabled the system calls UITableViewCell
's systemLayoutSizeFittingSize:withHorizontalFittingPriority:verticalFittingPriority: method to calculate the cell height, so you can subclass UITableViewCell
and override this method to force a minimum height for the cell, in this way
class MinHeightTableViewCell: UITableViewCell {
var minHeight: CGFloat?
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
let size = super.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: horizontalFittingPriority, verticalFittingPriority: verticalFittingPriority)
guard let minHeight = minHeight else { return size }
return CGSize(width: size.width, height: max(size.height, minHeight))
}
}
然后
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "minHeightCell", for: indexPath) as! MinHeightTableViewCell
cell.minHeight = 62 // change with your desidered min height
cell.textLabel?.text = "some text"
return cell
}
这篇关于UITableViewCell的最小高度,具有自动调整单元格的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!