UICollectionViewCompositionalLayout set Header via Configuration leads to Crash

I am trying to use UICollectionViewCompositionalLayout and would like to add a sticky global header to the whole collection view.

As far as I understand the documentation, this can be done via the UICollectionViewCompositionalLayoutConfiguration by adding a boundarySupplementaryItem and by setting pinToVisibleBounds to true.

I tried this and used the following layout:

    let layout = UICollectionViewCompositionalLayout.list(
        using: .init(appearance: .plain)
    )
    let supplementary = NSCollectionLayoutBoundarySupplementaryItem(
        layoutSize: .init(
            widthDimension: .fractionalWidth(1), 
            heightDimension: .absolute(50)
        ),
        elementKind: UICollectionView.elementKindSectionHeader,
        alignment: .top
    )

    let configuration = UICollectionViewCompositionalLayoutConfiguration()
    supplementary.pinToVisibleBounds = true
    configuration.boundarySupplementaryItems = [supplementary]
    layout.configuration = configuration

This works just fine, but if there are no items in the collection view and I scroll the content (and especially the global header) out of the viewport, I encounter a weird crash which I am not sure how to fix or if I can fix it.

The crash is:

2022-06-13 14:20:57.001033+0200 CompositonalLayoutGlobalHeaderCrash[79529:2256455] *** Assertion failure in CGRect _UIPinnedFrameForFrameWithContainerFrameVisibleFrame(CGRect, CGRect, CGRect, NSRectAlignment)(), _UICollectionLayoutHelpers.m:733

2022-06-13 14:20:57.012169+0200 CompositonalLayoutGlobalHeaderCrash[79529:2256455] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Frame {{0, 50}, {375, 0}} does not intersect {{0, 50}, {375, 0}}'

For me it seems like there is a assertion in the layout which checks that the global header does not leave the frame of the collection views content. Is this a bug of UICompositionalLayout or is there someone who can help fixing this?

I've also filed a radar: FB10224177

Hi! I faced the same issue and setting alwaysBounceVertical to false when there are no items and back to true when collection view has items to display right before applying the data from the snapshot to the diffable data source helped to fix the issue:

collectionView.alwaysBounceVertical = !collectionViewItems.isEmpty
UICollectionViewCompositionalLayout set Header via Configuration leads to Crash
 
 
Q