UICollectionViewCompositionalLayout incorrect item size

Car*_*ten 6 ios uicollectionview uicollectionviewlayout swift uicollectionviewcompositionallayout

I'm currently facing an issue with compositional layout which can also be seen in Apple's example -> ConferenceNewsFeedViewController. The item height is set to .estimated(100) and the width is set to .fractionalWidth(1.0). On the initial display the cell is not wrapping the label content. Only if you begin to scroll the layout of the cells are corrected. Does anybody know how to fix this issue?

Here is the incorrect layout:

布局不正确

Here is the layout after scrolling:

正确的布局

I know that it can be "fixed" with the following, but this feels more like a dirty work around:

DispatchQueue.main.async {
            self.collectionView.collectionViewLayout.invalidateLayout()
        }
Run Code Online (Sandbox Code Playgroud)

Any help to this is appreciated.

Best, Carsten

小智 -3

您可以使用此方法来设置单元格的大小。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: width, height: height)
Run Code Online (Sandbox Code Playgroud)

}