UICollectionView sticky header disappears for while after inserting section when collection is overscrolled (bounce effect)

sectionfooterspintovisiblebounds
uicollectionviewflowlayout
uicollectionview using horizontal and vertical scrolling with sticky rows and columns
uicollectionview section header
layoutattributesforsupplementaryview
uicollectionview section header swift 4
uicollectionview header
uicollectionview section header programmatically swift 4

I'm using UICollectionReusableView as a header of UICollectionView section. I enabled "sticky headers" with:

let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout
layout?.sectionHeadersPinToVisibleBounds = true

I'm inserting new sections to collection with:

collectionView.performBatchUpdates({
    self.collectionView.insertSections(IndexSet(integersIn: collectionView.numberOfSections...viewModel.numberOfSections - 1))
}, completion: nil)

If the insertion happens when collection is overscrolled (bounce is enabled), header will disappear for while (see GIF below). How can I avoid this behaviour?

I am using iOS 12.1.4 but same issue happens also on iOS 11.x and 12.x simulators.

The problem doesn't happen if bounce effect is turned off but I want to keep it on for smoother scroll feeling. I tried invalidate layout before/after update with no result. Thanks for advices.

EDIT (02/26/2019) Workaround: Wrapping the insertion to performWithoutAnimation block solve header disappearing but obviously disable reload animation.

UIView.performWithoutAnimation {
    collectionView.performBatchUpdates({
        self.collectionView.insertSections(IndexSet(integersIn: collectionView.numberOfSections...viewModel.numberOfSections - 1))
    }, completion: nil)
}

Unfortunately, by calling performBatchUpdates, the layout automatically animates all items itself. Even until now, there's no way to explicitly tell which items to animate and which not to.

However, I came up with a resolution which is kind of an anti-pattern.

For your header class, override these methods:

       -(void)setBounds:(CGRect)bounds
        {
            [CATransaction begin];
            [CATransaction setDisableActions:self.shouldDisableAnimations];
            [super setBounds:bounds];
            [CATransaction commit];
        }
    
        //-(void)setCenter:(CGPoint)center
    
    
        - (void)setCenter:(CGPoint)center
        {
            [CATransaction begin];
            [CATransaction setDisableActions:self.shouldDisableAnimations];
            [super setCenter:center];
            [CATransaction commit];
        }

48363057: UICollectionView sticky header , 48363057: UICollectionView sticky header disappears for while after inserting section when collection is overscrolled (bounce effect) #21042. You must adjust layout attributes for each header element, and you need to manually insert attributes for missing headers because by default, UICollectionViewFlowLayout only emits attributes for headers that are in the bounds. (UICollectionView looks at all the attributes and maps them to existing or newly-created views.) Here’s the code.


The above solution I provided earlier seems not to work on iOS 13.

During performBatchUpdates, UICollectionView doesn't apply layoutAttributes for elements. The only way to fix this is to make set the frame (or any other parameters) on the header explicitly from within prepare() method of the layout. Because there's an ongoing animation being performed, UICollectionView won't layout during the animation.

I think Apple has to add a feature to explicitly mark which elements participate in the animation and which not. This can be done through the layoutAttributes of elements or a separate method in UICollectionViewLayout.

Accessing the element instances from layout is a no-no! But there's no other workarounds at the moment.

How to Add Sticky Section Headers to a Collection View, Sticky section headers, for example, are built into table views. Adding them to a collection view requires a bit of extra work. The section headers are given a random color to make it easier to see the effect of the sticky section headers. the sticky section headers would disappear the moment they move  Sticky Headers at the top of a UICollectionView! to-make-supplementary-view-float-in-uicollectionview-as-section-headers-do-i -- // -- still needs work around


I was also having the same problem, but the work around I have done is reload the collectionView(the sticky header didn't disappears) it self instead of insert.

sectionHeadersPinToVisibleBounds, UICollectionView sticky header disappears for while after inserting section when collection is overscrolled (bounce effect). I'm using UICollectionReusableView  The section headers are given a random color to make it easier to see the effect of the sticky section headers. Subclassing UICollectionViewFlowLayout. The star player of this tutorial is the UICollectionViewFlowLayout class. To implement sticky section headers, we need to create a UICollectionViewFlowLayout subclass.


Uitableview scroll to top after reload, A Boolean value that indicates whether headers pin to the top of the collection view bounds during scrolling. Availability. iOS 9.0+; Mac Catalyst 13.0+; tvOS 9.0​+. A sticky header which disappears from view when not needed (ie: when the user is scrolling to see more content) is an excellent compromise. We can achieve this kind of effect by using an external library like Headroom.js , but we’re going to learn the mechanics of what’s underneath by building something ourselves.


ListView, android.animation See Using a Loader to learn how to avoid blocking the main thread when A class that represents a fixed view in a list, for example a header at the top or a set to false, the ListView will not draw the divider after each header view. This is called during layout when the size of this view has changed. 1. Setting up the Sticky Section Header. Configure your collection view to use CSStickyHeaderFlowLayout. Here's an example on how you to do it in Storyboard. Now all your section headers will get the sticky effect like table view. You can disable it with one line of code.


Pull to refresh on scroll view swift, Notice how much harder it is to pull to refresh on the animation below: new animation when a scroll view scrolled down (that is pulled down) and after A vertically scrolling collection view with one extra item added to show the section-​based Pull-to-refresh animation in UICollectionView with a sticky header flow layout,  When scrolling horizontally, the header bar should scroll as well. When scrolling vertically, the header bar should stick to the top. Here's a little mockup showing the initial calendar and the header bar (in yellow): This is how the calendar looks like after the user scrolled in both directions: Let's look at how we could implement this behavior.