强子D 2017-06-12 12:44 采纳率: 0%
浏览 561

estimatedRowHeight masonry

设置了estimatedRowHeight后,用masonry 计算出来的cell高度没问题,但是contentsize确有问题,
ContentSize是estimatedRowHeight的值 乘以cell的个数,有大神遇到吗?

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-05 23:50
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    这个问题是因为在iOS中,masonry计算cell的高度时,它会考虑到content size和estimated row height。如果内容大小大于estimated row height,则cell的高度会被调整为内容大小;否则,cell的高度仍保持为estimated row height。

    例如:

    let estimatedRowHeight = 50.0 // 设置的estimatedRowHeight
    let contentSize = CGSize(width: 100, height: 200) // 设置的内容大小
    
    if contentSize.height > estimatedRowHeight {
        cell.frame.size.height = contentSize.height
    } else {
        cell.frame.size.height = estimatedRowHeight
    }
    

    如果你发现内容大小大于estimated row height,但cell的高度还是按照estimated row height来设置的,那么你需要检查你的masonry布局是否正确。你可以在layoutSubviews方法中添加以下代码:

    func layoutSubviews() {
        super.layoutSubviews()
        
        if contentSize.height > estimatedRowHeight {
            cell.frame.size.height = contentSize.height
        } else {
            cell.frame.size.height = estimatedRowHeight
        }
    }
    

    这样,即使内容大小大于estimated row height,cell的高度也会根据内容大小进行调整。

    评论

报告相同问题?