Premature UICollectionView re-layout when animating its frame [iOS 11][iOS 12]

Originator:misacek
Number:rdar://42390719 Date Originated:July 19 2018, 7:59 PM
Status:Open Resolved:
Product:UIKit Product Version:12
Classification:UI / Usability Reproducible:Always
 
Area:
UIKit

Summary:

UICollectionView fails to continuously layout the included cells when changing its frame within an animation block. It fails in Objective C completely and a possible workaround in Swift seems to be unexpected the way it's done.

I'm providing the issue demonstration in both Swift (workaround-able) and Objective C (failing).

A few important related lines are marked with `// MARK:` comments in both examples.

# Swift demo – playground:

Swift demo contains an illustration of a loop performing expanding and shrinking of the holding view using an animation. Included table view and collection view have auto resizing mask applied so when I start changing the holder view's height using an animation block, both elements change their height using an animation properly.

Contracting (setting a lower height) phase is the important one where the issue is visible. While the table view lays out its content during the transition smoothly (cells stay in place until they get of bounds), the collection view immediately calls for re-layout, dropping the to-be-out-of-bounds cells immediately, before the animation block finishes (`contractWrong()` function).

Now, I've discovered that by moving the frame update operation inside the animation block addresses the issue on Swift (as seen in `contractRight()` function).

Assigning the updated frame struct to a holder view's frame property is included within the animation block in BOTH functions.

My first Question no.1 here then is why do these functions even behave differently? I'd like to know as it would probably describe some unexpected behaviour here.

# Objective C demo – example project:

Objective C example is amost-a-1:1 copy of the Playground code. Unlike the Swift example and its workaround, I cannot achieve the workarounding hotfix behaviour of `-contractRight` in Objective C.

Thus there comes my Question no.2 – how am I supposed to do this properly then?

As the Swift example tends to be behaving a bit unexpectedly due to a single line position and the Objective C code is not working at all, with no easy way to alter the expected behaviour the work, I consider this being a Collection view layout issue.

Any hints on how to work around the issue in Objective C and/or bug confirmation would be welcome.
_______________________________

Steps to Reproduce:
See the attached demos and the description above.

Expected Results:
A smooth transition of collection view cells.

Actual Results:
Premature re-layout performing a cutting effect when contracting the frame.

Version/Build:
iOS 11/12

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!