Swift如何制作uicollectionviewcell';基于内容的高度变化

Swift如何制作uicollectionviewcell';基于内容的高度变化,swift,Swift,我有这个Swift密码 let collectionView = UICollectionView(frame: CGRect(x:0,y:0,width:0,height:0), collectionViewLayout: UICollectionViewFlowLayout()) collectionView.translatesAutoresizingMaskIntoConstraints = false collectionView.register(PostCell.self, fo

我有这个Swift密码

let collectionView = UICollectionView(frame: CGRect(x:0,y:0,width:0,height:0), 
collectionViewLayout: UICollectionViewFlowLayout())
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.register(PostCell.self, forCellWithReuseIdentifier: cellId)
collectionView.delegate = self
collectionView.dataSource = self
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PostCell
        return cell
    }

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
    return CGSize(width: self.view.frame.size.width-30, height: 50)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
    return UIEdgeInsets(top: 15, left: 5, bottom: 15, right: 5)
}
我的目标是根据单元格的内容自动设置单元格的高度。我想实现
self.flowLayout.estimatedSize=UICollectionViewFlowLayoutAutomaticSize
,但我不知道什么是
flowLayout

如果要获取flowLayout:

if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
    flowLayout.estimatedItemSize = CGSize(width: 100, height: 100) //use auto layout for the collection view
}
您可以这样做:

let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.estimatedItemSize = CGSize(width: 100, height: 100)
let collectionView = UICollectionView(frame: CGRect(x:0,y:0,width:0,height:0), collectionViewLayout: layout)

如果要获取flowLayout:

if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
    flowLayout.estimatedItemSize = CGSize(width: 100, height: 100) //use auto layout for the collection view
}
您可以这样做:

let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.estimatedItemSize = CGSize(width: 100, height: 100)
let collectionView = UICollectionView(frame: CGRect(x:0,y:0,width:0,height:0), collectionViewLayout: layout)

是否需要将
let collectionView=UICollectionView(帧:CGRect(x:0,y:0,宽度:0,高度:0),collectionViewLayout:UICollectionViewFlowLayout())更改为
let collectionView=UICollectionView(帧:CGRect(x:0,y:0,宽度:0,高度:0),collectionViewLayout:flowLayout)
这不是必需的,因为此代码仅更改estimatedItemSIze,但它是否仅与UICollectionViewFlowLayout()一起工作?是的,或者您可以执行类似于我更新的回答中的操作不幸的是,此代码显示了一个207乘207的正方形,我也尝试了你的代码它没有任何结果也没有
UICollectionViewFlowLayoutAutomaticSize
是否有必要将
let collectionView=UICollectionView(frame:cRect(x:0,y:0,width:0,height:0),collectionViewLayout:UICollectionViewFlowLayout())更改为
let collectionView=UICollectionView(frame:cRect(x:0,y:0,宽度:0,高度:0),collectionViewLayout:flowLayout)
这不是必需的,因为此代码仅更改estimatedItemSIze,但它仅与UICollectionViewFlowLayout()一起工作?是的,或者你可以在我的更新答案中执行类似操作不幸的是,此代码显示一个207乘207的正方形,我尝试了你的代码,但也没有任何结果,UICollectionViewFlowLayoutAutomaticSize也没有