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 
    } 
相關問題