0

我有一个包含图像视图和标签的UICollectionViewCell。动态设置UICollectionViewCell的大小

只有当我有图像视图时,才能动态调整单元格大小。

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

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

以下是我的代码,只有与ImageView的

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

我有文字和图片从我在哪里显示内容的数组工作。

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

+0

它可以帮助你https://possiblemobile.com/2016/02/sizing-uicollectionviewcell-fit-multiline-uilabel/ –

回答

0

标签的宽度=图像视图的宽度。 没关系。 为高度,您可以使用Intrinsic Content Size函数来获取标签的大小。

0
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) 
    } 
0

这里collectioncell是Collection视图出口

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize { 

     let cellsize = CGSize(width: (collectioncell.bounds.size.width/2) - 12, height:(collectioncell.bounds.size.height/3) - 20) 

     return cellsize 
    }