UITableViewCell的最小高度,具有自动调整单元格的大小 [英] UITableViewCell minimum height with automatically resizing cells

查看:322
本文介绍了UITableViewCell的最小高度,具有自动调整单元格的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用自动调整单元格的大小.我想将 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

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的最小高度,具有自动调整单元格的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆