UICollectionView's `scrollToItemAtIndexPath` does not work when using self sizing cells

Originator:bryan.keller
Number:rdar://37836568 Date Originated:Feb 23, 2018
Status:Open Resolved:
Product:iOS / UIKit Product Version:iOS 11
Classification:Bug Reproducible:
 
Area:
UIKit

Summary:
When using self sizing cells with UICollectionView (either a custom layout or UICollectionViewFlowLayout), scrollToItemAtIndexPath does not reliably scroll to the intended position. The bug occurs because collection view does not size cells leading up to the desired index path, and uses an estimated cell height to figure out the correct offset instead.

Additionally, if the user scrolls up toward the top of the page after scrolling to a self sized item, items above begin to receive their final sizes, resulting in jumpy scrolling.

Steps to Reproduce:
(See attached Playground sample project)
1. Wire up a collection view + flow layout and configure it to use self sizing cells.
2. Display enough cells to extend well beyond the height of the collection view
3. Call scrollToItemAtIndexPath with a large index path (far away from visible index paths)

Expected Results:
The collection view should scroll to the correct item

Actual Results:
The collection view scrolls to an item that's roughly near the desired index path, but isn't exact. If the user scrolls up, items begin to layout and receive their final size, resulting in visually jumpy scrolling.

Version/Build:
iOS 11

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!