6

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:

incorrect layout

Here is the layout after scrolling:

correct layout

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()
        }

Any help to this is appreciated.

Best, Carsten

Carsten
  • 581
  • 4
  • 17
  • Hey Carsten, do you know if this issue is still unresolved by Apple? Is there a radar ticket somewhere I can follow? I'm seeing this with Xcode 12.5 and iOS 14.6 after not having this issue with previous versions. Thanks – dank_muffin Aug 15 '21 at 20:06
  • @dank_muffin I never followed up on this, sorry – Carsten Aug 17 '21 at 11:13
  • The bug is definitely still present (Xcode 14.2, iOS 16.2). All signs suggest that UICollectionViewCompositionalLayout is not production ready, unfortunately. –  Jan 24 '23 at 20:06

3 Answers3

0

I've spent days looking into this issue, trying everything I could find and think of.

Some other cells in the collection view were self sizing just fine, only a couple didn't. They had similar layout and content logic. The thing that miraculously made it work was removing the prepareForReuse() method from the collection view cell.

-1

I've been facing the same issue myself. In my case, I have the following setup:

  • CellSubclass: UICollectionViewListCell
    • CellContentView: UIContentView

in my CellContentView, I've overridden the layoutSubviews as such:

override func layoutSubviews() {
    super.layoutSubviews()
    invalidateIntrinsicContentSize()   
}

There are still some minor glitches sometimes, but this is better than having the wrong layout on load.

dezinezync
  • 2,730
  • 21
  • 15
-3

you can use this method for set size for your cells.

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

}

  • I want to use compositional layout with self sizing cells. Setting an explicit size will not help here – Carsten Oct 13 '20 at 13:03