2017-08-13 48 views
2

我有這個uicollectionviewcell斯威夫特給予uicollectionviewcell寬度和動態高度

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = commentSection.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! CommentCell 
    return cell 
} 

CommentCell

let CommenterprofileImage: UIImageView = { 
     let v = UIImageView() 
     v.translatesAutoresizingMaskIntoConstraints = false 
     return v 
    }() 
    let commentText: UILabel = { 
     let l = UILabel() 
     l.numberOfLines = 0 
     l.text = "some text" 
     l.translatesAutoresizingMaskIntoConstraints = false 
     return l 
    }() 
    override init(frame: CGRect) { 
     super.init(frame: frame) 
     backgroundColor = .blue 
     designCell() 
    } 
    func designCell(){ 

     addSubview(CommenterprofileImage) 
     addSubview(commentText) 

     addCellConstraints() 
    } 

    func addCellConstraints(){ 
     CommenterprofileImage.topAnchor.constraint(equalTo: self.topAnchor,constant:10).isActive = true 
     CommenterprofileImage.leftAnchor.constraint(equalTo: self.leftAnchor,constant:20).isActive = true 
     CommenterprofileImage.widthAnchor.constraint(equalToConstant: 70).isActive = true 
     CommenterprofileImage.heightAnchor.constraint(equalToConstant: 70).isActive = true 

     commentText.topAnchor.constraint(equalTo: CommenterprofileImage.bottomAnchor,constant:20).isActive = true 
     commentText.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true 
     commentText.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true 
    } 

,我想這個單元格的寬度等於view的寬度,但我想它是高度取決於它的內容。 我試着這樣做

layout.estimatedItemSize = CGSize(width: view.frame.width, height: 100) 

但後來我的手機的身高爲100,寬度小於100

+0

你可以用字符串的有界矩形方法計算標籤高度,這會給你估計的標籤高度,因爲作爲一個內容它是一個變化,並獲得e單元格的xact高度只是將您的圖片高度與估計的高度+ y位置約束相加。其他不錯的選擇是使用UICollectionViewLayout。 –

+0

@TusharSharma有界的矩形方法返回0 –

+0

顯示代碼你怎麼做? –

回答

0

你能做到這way-:

計算標籤的高度與boundingRect方法一線:

func estimatedFrameHeight(text:String) -> CGRect{ 
     let size = CGSize(width: (view.frame.width/2 - 8), height: 1000) 
     let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin) 
     let attribute = [NSFontAttributeName:UIFont.systemFont(ofSize: 11)] 
     return NSString(string: text).boundingRect(with: size, options: options, attributes: attribute, context: nil) 
    } 

請您提供您的標籤完全一樣的字體,並保持.usesLineFragmentOrigin多行標籤。

現在的CollectionView sizeForItemAt indexPath做這個 - :

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     let width = (collectionView.frame.width) 
     let subDict = subCategoryListArray[indexPath.row] 
     if let product_name = subDict["product_name"] as? String { 
     let height = estimatedFrameHeight(text:product_name) 
     return CGSize(width: width, height: height.height + 70 + 10 + 20) 
    } 
     return CGSize(width: 0, height: 0) 

    } 

let height = estimatedFrameHeight(text:product_name)。如果需要,這將爲您提供估計的高度和寬度。現在,你有你的標籤高度計算需要添加ImageView的高度,y位置的限制,使其work.You可以從這裏 - 得到它:

CommenterprofileImage.heightAnchor.constraint(equalToConstant: 70).isActive = true 

Height = 70 you have. 

而且,

CommenterprofileImage.topAnchor.constraint(equalTo: self.topAnchor,constant:10).isActive = true 

commentText.topAnchor.constraint(equalTo: CommenterprofileImage.bottomAnchor,constant:20).isActive 

頂部限制爲= 10

頂部限制條件是= 20

所以你需要添加這個,現在你可以看到我的答案一樣。

Remark-:

只添加ImageView的身高,如果是上面的標籤別的不需要,只是 增加高度和y填充你有上述標籤什麼都。

相同的計算準確的寬度爲標籤subtarct領先或尾隨 空間。

如果標籤覆蓋完整的寬度(無需插圖減法) - :

func estimatedFrameHeight(text:String) -> CGRect{ 
     let size = CGSize(width: (view.frame.width), height: 1000) 
     let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin) 
     let attribute = [NSFontAttributeName:UIFont.systemFont(ofSize: 11)] 
     return NSString(string: text).boundingRect(with: size, options: options, attributes: attribute, context: nil) 
    } 

如果標籤是20分領先,然後減去但─:

func estimatedFrameHeight(text:String) -> CGRect{ 
      let size = CGSize(width: (view.frame.width - 20), height: 1000) 
      let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin) 
      let attribute = [NSFontAttributeName:UIFont.systemFont(ofSize: 11)] 
      return NSString(string: text).boundingRect(with: size, options: options, attributes: attribute, context: nil) 
     }