UICollectionView with self sizing cells lays out incorrectly

Originator:maximilianfc
Number:rdar://46236349 Date Originated:26/11/2018
Status:New Resolved:
Product:iOS SDK UIKit Product Version:12.1
Classification: Reproducible:Always
 
Summary: UICollectionView asks its cells for preferred attributes and the cells give incorrect sizes. A second invalidation and layout is needed for cells to return more correct sizes. And then a third invalidation and layout is needed for the truly correct sizes to be given.

Steps to Reproduce:
1. Set up a UICollectionViewController (or UIViewController with a UICollectionView). Give the it a flow layout with estimated item size.
2. Set up a cell that requires a dynamic size. For example a cell with a multiline label.
3. Use this cell in the collection view and set varying text lengths on the label during the cellForItem data source calls.
4. Observe when the collection view is displayed that the cells all use the estimated size.
5. Invalidate the layout
6. Observe the collection view uses over estimated sizes.
7. Invalidate the layout.
8. Observe the collection view uses correct sizes.


Expected Results:
No extra invalidations should be necessary for the cells to offer correct preferred sizes. The collection view should ask the cells for preferred attributes when everything is in place for the preferred attributes to be correct.

Actual Results:
The cells return preferred sizes matching the give layout attributes first. On the next layout invalidation they give over estimated sizes. On the third invalidation they give correct sizes.

Version/Build:
Xcode 10.1 (10B61), iOS 12.1

Configuration:
See attached example project demonstrating the issue with 100% reproducibility.

Comments

Example project: https://drive.google.com/open?id=1WSFldrSHJT8CL-fHU_bqQ4xQl1L0Lm_f

By maximilianfc at Nov. 26, 2018, 6:04 a.m. (reply...)

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!