UICollectionView remove header after inserting a section

341 Views Asked by At

I have a few sections in my UICollectionViewController, and only the first one should have a header. How to nicely insert a section at the front while removing the header from the previous first section?

I already set referenceSizeForHeaderInSection to be .zero for non-first sections. And here is what I have so far. It works but the header is noticeably removed AFTER insertion animation is finished. I tried to put them together, but it just didn't work

self.collectionView?.performBatchUpdates({

    self.collectionView?.insertSections([0])
    self.collectionView?.insertItems(at: someIndexPaths)

}, completion: { (_) in

    let context = UICollectionViewFlowLayoutInvalidationContext()
    context.invalidateSupplementaryElements(ofKind: UICollectionElementKindSectionHeader, at: [IndexPath(item: 0, section: 1)])
    self.collectionView?.collectionViewLayout.invalidateLayout(with: context)

})
0

There are 0 best solutions below