UICollectionView 高度未根据内容正确更新。如何更新高度
我的代码:
collectionViewArray.append(trimmedStrig!)
DispatchQueue.main.async {
if self.collectionViewArray.count == 1 {
self.collectionViewHeight.constant = self.collectionView.contentSize.height + 50
print(self.collectionViewHeight.constant)
} else if self.collectionViewArray.count > 1 {
self.collectionViewHeight.constant = self.collectionView.contentSize.height
print(self.collectionViewHeight.constant)
}
self.collectionView.reloadData()
}
//Fix collection view cell height and width
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100, height: 50)
}
当创建第一个单元格时,它不显示该单元格,这就是为什么我添加了 50,当单元格再次进入第二行时,它不会更新高度,但是当在该时间创建的第二行第二个单元格高度更新时。为什么我不知道?
最佳答案
试试这个,它可能对你有帮助
self.collView.reloadData()
self.collView.performBatchUpdates({}) { (complition) in
self.cnsHeightCollView.constant = self.collView.contentSize.height
}
关于ios - 根据内容更新 UICollectionView 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57197989/