Ios UICollectionView中的动态宽度单元格

Ios UICollectionView中的动态宽度单元格,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我尝试使用UICollectionView实现标签。但是我必须用动态标签长度设置宽度。我试图消除图片中的空白。我分享一些代码。我用笔尖 提前谢谢 layoutFCV.estimatedItemSize = UICollectionViewFlowLayout.automaticSize let filterCV = UICollectionView(frame: self.view.bounds, collectionViewLayout: layoutFCV) -- 首先确保UICollec

我尝试使用
UICollectionView
实现标签。但是我必须用动态标签长度设置宽度。我试图消除图片中的空白。我分享一些代码。我用笔尖

提前谢谢

layoutFCV.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
let filterCV = UICollectionView(frame: self.view.bounds, collectionViewLayout: layoutFCV)
--


首先确保
UICollectionViewCell.xib
文件中的约束设置正确。我的意思是,随着细胞中
UILabel
的增长,细胞本身也应该增长

在获得单元格大小之前,应明确设置标题。下面是您的
collectionView(collectionView:,layout:,sizeForItemAt:)
方法应该是:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {       
    if collectionView == filterCollectionView {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FilterSelectionCollectionViewCell", for: indexPath) as! FilterSelectionCollectionViewCell
        cell.title.text = "Newest" //this is for the "Newest" cell. Of curse you should set the proper title for each indexPath
        cell.setNeedsLayout()
        cell.layoutIfNeede()
        return CGSize(width: cell.contenView.frame.width , height: cell.contentView.frame.height)
    }  else {
        return CGSize(width: 10, height: 10)
    }
}
使用此代码:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let text = "Title"
    let width = self.estimatedFrame(text: text, font: UIFont.systemFont(ofSize: 14)).width
    return CGSize(width: width, height: 50.0)
}

func estimatedFrame(text: String, font: UIFont) -> CGRect {
    let size = CGSize(width: 200, height: 1000) // temporary size
    let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
    return NSString(string: text).boundingRect(with: size,
                                               options: options,
                                               attributes: [NSFontAttributeName: font],
                                               context: nil)
}

分享你的UI设计你到底想要什么?我分享一张我想要的图片以防万一,在这里检查我的答案和结果示例添加一些解释单元格。layoutIfNeeded()不可见单元格你的意思是它使单元格不可见吗?它消失了?是的。如果删除layoutifneed,则会显示单元格。但此代码未设置宽度。这是因为在
xib
文件中未正确设置约束。我将更新我的答案,以显示如何设置约束Return CGSize(宽度:宽度+20,高度:50.0)对我来说,是的,你必须根据自己添加填充。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {       
    if collectionView == filterCollectionView {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FilterSelectionCollectionViewCell", for: indexPath) as! FilterSelectionCollectionViewCell
        cell.title.text = "Newest" //this is for the "Newest" cell. Of curse you should set the proper title for each indexPath
        cell.setNeedsLayout()
        cell.layoutIfNeede()
        return CGSize(width: cell.contenView.frame.width , height: cell.contentView.frame.height)
    }  else {
        return CGSize(width: 10, height: 10)
    }
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let text = "Title"
    let width = self.estimatedFrame(text: text, font: UIFont.systemFont(ofSize: 14)).width
    return CGSize(width: width, height: 50.0)
}

func estimatedFrame(text: String, font: UIFont) -> CGRect {
    let size = CGSize(width: 200, height: 1000) // temporary size
    let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
    return NSString(string: text).boundingRect(with: size,
                                               options: options,
                                               attributes: [NSFontAttributeName: font],
                                               context: nil)
}