我有一个 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/