Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reloading collectionview does not maintain content offset #56

Open
ChengmingLu opened this issue Sep 27, 2019 · 9 comments
Open

Reloading collectionview does not maintain content offset #56

ChengmingLu opened this issue Sep 27, 2019 · 9 comments
Assignees

Comments

@ChengmingLu
Copy link

Describe the bug
When reloadData() is called, the collectionView sometimes doesn't preserve the scrolling position, causing the collectionView to jump abruptly

To Reproduce
Steps to reproduce the behavior:

  1. Open and run the stock example project (workspacce) under MagazineLayoutExample
  2. Please see the attached gif to reproduce this issue
    Note: this doesn't occur every time, you need to adjust scrolling position before reloading the data if you cannot reproduce it at the beginning

Expected behavior
The screen should maintain its position after reloading data

Screenshots

Untitled

Smartphone (please complete the following information):

  • Simulator: iPhone 11 Pro Max
  • OS: iOS 13.0
  • Version: current

Additional context
Not sure if this could be a bug in UICollectionView rather than MagazineLayout, please advise.

@bryankeller bryankeller self-assigned this Oct 4, 2019
@Sparrow-Iv
Copy link

The problem is still relevant, is there any solution?

@eliotwjohnson
Copy link

We're experiencing this issue as well. Any time a reload occurs when the collection view is scrolled, its contentSize and contentOffset change, resulting in the jump. @bryankeller are there any known workarounds for this?

@ghost
Copy link

ghost commented Oct 6, 2020

I am also experiencing this issue. Whenever a reload happens, when the collection view is scrolled, its contentOffset change, resulting in the jump. @bryankeller is there any known fix/solution for the same?

@ghost
Copy link

ghost commented Oct 7, 2020

Seems like once we scroll the view and then on reload of data content size of collection view is not same as before even though data is same. Self sizing return different content size each time. I also looked into the item size returned in method preferredLayoutAttributesFitting which is same for items before and after reload of data.

@bryankeller does any one looking into this issue?

@maxcox
Copy link

maxcox commented Aug 10, 2021

This issue occurs in UICollectionView usually when implementing self-sizing cells but there are workarounds to get past it. Unfortunately the UICollectionViewDelegateFlowLayout methods we conform to in order to perform the workaround are not accessible when using MagazineLayout. Is there any possibility we can get a fix for this within MagazineLayout?

@Danie1s
Copy link

Danie1s commented Dec 22, 2021

use .static heightMode can solve this problem

@alexookah
Copy link

alexookah commented Jan 8, 2023

I am also experiencing this problem. Are there any plans to fix it?
Similar issue was happening on rotation and was fixed.
Maybe the same resolution could also be applied here.
@maxcox Could you share your UICollectionViewDelegateFlowLayout methods that get past this problem?

@elliot-vu
Copy link
Contributor

elliot-vu commented Apr 1, 2023

Good news! My PR is merged into the master branch. #113
I have a approach that cache collection view cell height when cell will appear. And input to sizeModeForItemAt delegate method. It look like this:

func collectionView(
    _ collectionView: UICollectionView,
    layout collectionViewLayout: UICollectionViewLayout,
    sizeModeForItemAt indexPath: IndexPath)
    -> MagazineLayoutItemSizeMode {
  if let cachedHeight = cachedHeightStorage[indexPath] {
     return MagazineLayoutItemSizeMode(widthMode: fullWidth(respectsHorizontalInsets: 10), heightMode: .dynamic(estimatedHeight : cachedHeight))
  }

  return MagazineLayoutItemSizeMode(widthMode: fullWidth(respectsHorizontalInsets: 10), heightMode: .dynamic)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants