Cell's autoresizing mask breaks self-sizing

Number:rdar://46865293 Date Originated:12/10/18
Status:Open Resolved:No
Product:UIKit Product Version:iOS 12
Classification:Bug Reproducible:Yes

For collection view cells / layouts who define cell width based on total available collection view width, a bug exists where a cell will size incorrectly on rotation. 

This is due to a cell creating incorrect constraints for `contentView` from its autoresizing mask. Despite `contentView`'s subviews being constrained correctly, and the correct parameters being passed into `systemLayoutSizeFitting` from `preferredLayoutAttributesFitting`, the contentView's autoresizing mask constraints will cause `systemLayoutSizeFitting` to return incorrect, stale values.

A workaround for the problem exists - simply setting `contentView.bounds = width` before invoking `systemLayoutSizeFitting` inside of `preferredLayoutAttributesFitting` will cause the autoresizing mask constraints to update their constant values, resulting in correct behavior when invoking `systemLayoutSizeFitting`.

In MagazineLayout, I need to work around this exact issue for rotation to work in all of our flows https://github.com/airbnb/MagazineLayout/blob/master/MagazineLayout/Public/Views/MagazineLayoutCollectionViewCell.swift

The included sample project demos the problem with flow layout.

Steps to Reproduce:
1. Open sample project
2. Run on iPhone XS simulator
3. Rotate from portrait to landscape - observe that there's extra padding vertical padding for some of the cells (the cell should hug the text closely on all sides)
4. Rotate back to portrait - observe more incorrectly sized cells

5. Uncomment Cell.swift:47 - this will fix the issue
6. Repeat 1-4, everything will behave correctly as long as that line (the fix) is present

I've included some print statements in `preferredLayoutAttributesFitting` to facilitate debugging.

Expected Results:
Cells should size correctly if their width changes and their heights are self-sizing.

Actual Results:
Cells size incorrectly if their width changes and their heights are self-sizing.

iOS 12


Sample project: https://drive.google.com/file/d/1dwdANQYmzu8ZyBQAaCFbux5Z3dF7e8Hl/view?usp=sharing


Appears to be fixed in iOS 14+

By kellerbryan19 at June 3, 2022, 7:55 p.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!