gpt4 book ai didi

ios - 动态设置 UICollectionViewCell 的大小

转载 作者:行者123 更新时间:2023-11-28 15:33:38 25 4
gpt4 key购买 nike

我有一个 UICollectionViewCell,它包含一个 ImageView 和一个标签。

只有当我有 ImageView 时,我才能动态调整单元格的大小。

我在带有自动布局的 imageView 下方添加了一个标签。

在 viewDidLoad 上,图像被截断或文本被截断。如何在 UICollectionViewCell 中动态调整 ImageView 和标签的大小。

以下是我的代码,仅适用于 imageView

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
let imageSize = model.images[indexPath.row].size
return imageSize
}

我在显示内容的位置有一组文本和图像。

任何帮助将不胜感激。谢谢。

最佳答案

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
let imageSize = model.images[indexPath.row].size

//find the height of label on setting text.
//create a temp label
let tempLabel = UILabel()
tempLabel.numberOfLines = 0
tempLabel.text = "abcd" //set ur cells text here
let labelSize = tempLabel.intrinsicContentSize

return CGSize(width: imageSize.width + labelSize.width, height: imageSize.height + labelSize.height)
}

关于ios - 动态设置 UICollectionViewCell 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44517014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com