Hide header animated from UICollectionViewCompositionalLayout

Hello!

I have a collectionView and assigned a layout to it:

collectionView.collectionViewLayout = createLayout(hasHeader: true)

func createLayout(hasHeader: Bool) -> UICollectionViewCompositionalLayout {
   let layout = UICollectionViewCompositionalLayout { [weak self] (section,environment) -> NSCollectionLayoutSection? in
   // configure cells
   }
   // adding a header:
   if hasHeader {
      let header = //...
      layout.boundarySupplementaryItems.append(header)
   }
   return layout
}

Now, I just want to hide the header (animated).

Removing the header can simply be done this way, but this is not animated:

collectionView.collectionViewLayout = createLayout(hasHeader: false)

Is there any other possibility to hide it animated?

Answered by Engineer in 817649022

If your header is a view with constraints you can try modifying the constraint and reloading the view to animate the constraint change. You won't have explicit control over the animation curve but you can force a system controlled animation in response to constraint changes.

There may be a better way to do this, but without additional information, conceptually you can do something like this.

Accepted Answer

If your header is a view with constraints you can try modifying the constraint and reloading the view to animate the constraint change. You won't have explicit control over the animation curve but you can force a system controlled animation in response to constraint changes.

There may be a better way to do this, but without additional information, conceptually you can do something like this.

Hide header animated from UICollectionViewCompositionalLayout
 
 
Q