iOS 16 collectionView layoutAttributesForItem return the wrong size with estimated height compositional layout

425 Views Asked by At

I ran into an ios 16 problem when layoutAttributesForItem returns a very small cell size if it is far beyond the scope and visibility and has not yet been seen.

I'm using compositional layout with estimated(1) and it feels like its real size is not calculated before it appears on the screen

So I set the section:

`

     func getListLayout() -> NSCollectionLayoutSection {
        let itemSize = NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(1),
            heightDimension: .estimated(1)
        )
        let item = NSCollectionLayoutItem(layoutSize: itemSize)

        let groupSize = NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(1),
            heightDimension: .estimated(1)
        )

        let group = NSCollectionLayoutGroup.horizontal(
            layoutSize: groupSize,
            subitem: item,
            count: 1
        )

        group.contentInsets = .zero

        let headerFooterSize = NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(1),
            heightDimension: .estimated(headerHeight)
        )

        let section = NSCollectionLayoutSection(group: group)
        section.contentInsets = .zero
        section.boundarySupplementaryItems = [sectionHeader]

        return section
    }

Wrong result with:

let attributes = collectionView.layoutAttributesForItem(at: indexPath)
attributes.frame.minY

IOS 14-15 everything works well..

I noticed that if you set a specific height for the cell (absolute), everything works as it should, but I need the height to be considered dynamically. Example: enter image description here

0

There are 0 best solutions below