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

Prevent iOS CollectionView size shifts from clearing the cell size cache #18464

Merged
merged 2 commits into from
Nov 22, 2023

Conversation

hartez
Copy link
Contributor

@hartez hartez commented Nov 1, 2023

Description of Change

In some circumstances, the iOS CollectionView runs into an issue where rounding/floating point math differences cause the UICollectionViewLayout to trigger a bounds-change layout incorrectly. This can happen continuously during scrolling operations, which causes the cell size cache to repeatedly clear, which in turn causes constant cell resizing and shifting.

These changes eliminate one source of diverging size information and widen the tolerance for other layout triggers, preventing the continuous cache clearing. This also introduces an automated test to ensure the cell size cache is active.

Issues Fixed

Fixes #17890

@maonaoda
Copy link
Contributor

maonaoda commented Nov 2, 2023

It seems that this problem also exists on other view on iOS.

When I tried to solve this #17885, I found that there was always a very small gap in the size before and after, although the size did not change.

    public class HeaderFooterLayoutView : LayoutView
    {
        public override void LayoutSubviews()
        {
            base.LayoutSubviews();

            var bounds = AdjustForSafeArea(Bounds).ToRectangle();

            var widthConstraint = bounds.Width;
            var heightConstraint = bounds.Height;

            var size = CrossPlatformLayout.CrossPlatformMeasure(widthConstraint, double.PositiveInfinity);
            if (CrossPlatformLayout is VisualElement element
                && element.Parent is StructuredItemsView
                && (Math.Round(heightConstraint, 3, MidpointRounding.ToZero) != Math.Round(size.Height, 3, MidpointRounding.ToZero)))
            {
                element.InvalidateMeasureNonVirtual(Microsoft.Maui.Controls.Internals.InvalidationTrigger.Undefined);
            }
        }
    }

@Eilon Eilon added the area-controls-collectionview CollectionView, CarouselView, IndicatorView label Nov 2, 2023
@hartez hartez marked this pull request as ready for review November 8, 2023 22:40
@hartez hartez requested a review from a team as a code owner November 8, 2023 22:40
@PureWeen PureWeen requested review from mattleibow and Redth and removed request for jsuarezruiz November 9, 2023 10:49
@Redth
Copy link
Member

Redth commented Nov 20, 2023

/rebase

@rmarinho rmarinho self-requested a review November 21, 2023 19:55
@PureWeen PureWeen merged commit 6a5bbf0 into main Nov 22, 2023
47 checks passed
@PureWeen PureWeen deleted the fix-17890 branch November 22, 2023 21:54
rmarinho added a commit that referenced this pull request Nov 23, 2023
rmarinho added a commit that referenced this pull request Nov 27, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Dec 23, 2023
@samhouts samhouts added the fixed-in-8.0.6 Look for this fix in 8.0.6 SR1! label Aug 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView fixed-in-8.0.6 Look for this fix in 8.0.6 SR1! platform/iOS 🍎
Projects
None yet
7 participants