Repository navigation
[iOS/MacCatalyst] Keep CollectionView following its edge when UIKit animates the view back as it lays it out - #40854
Conversation
…nimates the view back as it lays it out When the content of a UICollectionView shrinks under its content offset during a layout pass, UIKit animates the view back within the content: -[UICollectionView _setContentSize:skipContentOffsetAdjustment:] wraps the content size update in performWhileAnimatingAutomaticContentOffset- Adjustments:, and -[UIScrollView _adjustContentOffsetIfNecessary] then calls setContentOffset:animated:YES. The scroll anchor took every scroll animation it didn't start for a fling: it stopped correcting, and, as UIKit measured the rows the animation brought into view and re-targeted it, it judged the view to have moved away from the edge and stopped following it. With rows shorter than UIKit's 30-point estimate, a list with AnchorMode End mostly failed to open at its end, and otherwise lost it when a batch of such rows arrived. The anchor now notes whether a scroll animation is running when a layout pass starts. If one runs when the pass is done that didn't when it started, and neither the app nor the anchor started one, UIKit started it as it laid the view out: a view that follows an edge stops it where it starts and goes to the edge, as for any other layout change. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This comment has been minimized.
This comment has been minimized.
|
@kubaflo, could you review this? It's in the same scroll-anchor family as #40817 and #40828. It touches only The evidence, in short:
One more finding, not changed here: with macOS keyboard navigation on, the focus engine's |
There was a problem hiding this comment.
🟡 Changes recommended
The scroll-anchor logic can cancel deliberate native scroll animations during layout even without content shrinkage, requiring a narrower cancellation condition and a regression test.
1 open issue
What changed in this PR
This PR updates the iOS/Mac Catalyst CollectionView handler to preserve edge anchoring when UIKit starts an automatic scroll animation during layout.
Changes:
- Tracks animation state at the start of layout and stops newly detected animations under edge-following conditions.
- Passes the collection view into all three layout-start hooks.
- Adds a device regression test for opening at—and continuing to follow—the end of rows shorter than UIKit’s estimates.
| File | Description |
|---|---|
src/Controls/tests/DeviceTests/Elements/CollectionView/CollectionViewTests.ScrollAnchor.cs |
Tests end anchoring with short rows and appended items. |
src/Controls/src/Core/Handlers/Items2/iOS/ItemsViewController2.cs |
Supplies the native view to layout-start tracking. |
src/Controls/src/Core/Handlers/Items2/iOS/CollectionViewScrollAnchor.cs |
Detects and stops animations begun during layout while following an edge. |
Review effort: Balanced
| if (!_animatingBeforeLayout && isVertical && _tracker.IsAnchored && _edgeAnimation is null && !_animatingRequest | ||
| && !collectionView.Dragging && !collectionView.Decelerating && IsScrollAnimating(collectionView)) | ||
| { | ||
| ApplyOffset(collectionView, collectionView.ContentOffset.Y); |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Selected Tests
🧪 Test selection — click to expand
🧪 UI tests —
|
| Platform | Categories |
|---|---|
| iOS | CarouselView, CollectionView1, CollectionView2, CollectionView3, CollectionView4, CollectionView5, CollectionView6, CollectionView7 |
| MacCatalyst | CarouselView, CollectionView1, CollectionView2, CollectionView3, CollectionView4, CollectionView5, CollectionView6, CollectionView7 |
🧪 Device tests — Controls.CarouselView, Controls.CollectionView on iOS, MacCatalyst
❌ Could not queue maui-pr-devicetests: Azure Pipelines did not start maui-pr-devicetests for the dispatch branch run-devicetests/pr40854-46406207908 within 10 minutes. Comment /review run-tests again, or use /azp run maui-pr-devicetests for the full suite.
GPT-6.2 Sol: Run CollectionView and CarouselView device tests on iOS and Mac Catalyst, including the new scroll-anchor regression.
MAI Code 2: Run CollectionView and coupled CarouselView device tests on iOS and Mac Catalyst for the Items2 anchor-animation fix.
🧪 Running — 2 categories on iOS, MacCatalyst
| Platform | Categories |
|---|---|
| iOS | Controls.CarouselView, Controls.CollectionView |
| MacCatalyst | Controls.CarouselView, Controls.CollectionView |
⏭️ Skipped — what neither suite runs
UI tests — 69 categories, 2 platforms
Platforms not run: Android, Windows
Categories not run: Accessibility, ActionSheet, ActivityIndicator, Animation, AppLinks, Border, BoxView, Brush, Button, Cells, CheckBox, ContextActions, CustomRenderers, DatePicker, Dispatcher, DisplayAlert, DisplayPrompt, DragAndDrop, Entry, Editor, Effects, Essentials, FlyoutPage, Focus, Fonts, Frame, Gestures, GraphicsView, Image, ImageButton, IndicatorView, InputTransparent, IsEnabled, IsVisible, Label, Layout, Lifecycle, ManualReview, Maps, Navigation, Page, Performance, Picker, ProgressBar, RadioButton, RefreshView, SafeAreaEdges, Shadow, ScrollView, SearchBar, Shape, Slider, SoftInput, Stepper, Switch, SwipeView, Shell1, Shell2, TabbedPage, TableView, TimePicker, TitleView, ToolbarItem, Triggers, ViewBaseTests, VisualStateManager, Window, WebView, Material3
Each skipped platform and category is listed as a maui-pr-uitests (skipped) … status on this commit. Use /azp run maui-pr-uitests for the full suite.
Device tests — 103 categories, 2 platforms
Platforms not run: Android, Windows
Categories not run: Controls.Accessibility, Controls.Animations, Controls.Application, Controls.Behavior, Controls.Border, Controls.BoxView, Controls.Button, Controls.CheckBox, Controls.Compatibility, Controls.ContentView, Controls.DatePicker, Controls.Dispatcher, Controls.Editor, Controls.Element, Controls.Entry, Controls.FlexLayout, Controls.FlyoutPage, Controls.Frame, Controls.Gesture, Controls.HybridWebView, Controls.Image, Controls.Label, Controls.Layout, Controls.Lifecycle, Controls.Map, Controls.Mapper, Controls.Memory, Controls.MenuFlyout, Controls.Modal, Controls.NavigationPage, Controls.Page, Controls.PageTransitions, Controls.Path, Controls.Picker, Controls.RadioButton, Controls.RefreshView, Controls.ScrollView, Controls.SearchBar, Controls.Shape, Controls.Shell, Controls.Slider, Controls.SwipeView, Controls.Switch, Controls.TabbedPage, Controls.TemplatedView, Controls.TextInput, Controls.Toolbar, Controls.View, Controls.VisualElement, Controls.VisualElementTree, Controls.WebView, Controls.Window, Controls.WindowOverlay, Controls.Xaml, Core.ActivityIndicator, Core.Animation, Core.Application, Core.Border, Core.BoxView, Core.Button, Core.CheckBox, Core.ContentView, Core.DatePicker, Core.Dispatcher, Core.Editor, Core.Element, Core.Entry, Core.FlowDirection, Core.FlyoutView, Core.Fonts, Core.Formatting, Core.Graphics, Core.GraphicsView, Core.Image, Core.ImageButton, Core.ImageSource, Core.IndicatorView, Core.Label, Core.Layout, Core.Lifecycle, Core.MauiContext, Core.Memory, Core.NavigationPage, Core.Page, Core.Picker, Core.ProgressBar, Core.RadioButton, Core.RefreshView, Core.ScrollView, Core.SearchBar, Core.ShapeView, Core.Slider, Core.Stepper, Core.SwipeView, Core.Switch, Core.TimePicker, Core.View, Core.WebView, Core.Window, Core.WindowOverlay, Graphics, Essentials, BlazorWebView
Each skipped platform and project is listed as a maui-pr-devicetests (skipped) … status on this commit. Use /azp run maui-pr-devicetests for the full suite.
🤖 Why these tests — AI rationale (GPT-6.2 Sol + MAI Code 2)
UI tests
Each model chose independently; every test that runs was selected by at least one of them.
| Platform | GPT-6.2 Sol | MAI Code 2 |
|---|---|---|
| Android | — | — |
| iOS | CollectionView1, CollectionView2, CollectionView3, CollectionView4, CollectionView5, CollectionView6, CollectionView7, CarouselView |
CollectionView1, CollectionView2, CollectionView3, CollectionView4, CollectionView5, CollectionView6, CollectionView7, CarouselView |
| MacCatalyst | CollectionView1, CollectionView2, CollectionView3, CollectionView4, CollectionView5, CollectionView6, CollectionView7, CarouselView |
CollectionView1, CollectionView2, CollectionView3, CollectionView4, CollectionView5, CollectionView6, CollectionView7, CarouselView |
| Windows | — | — |
GPT-6.2 Sol — Run CollectionView and CarouselView UI tests on iOS and Mac Catalyst for the Items2 scroll-anchor change.
- CollectionViewScrollAnchor changes how UIKit layout animations affect edge following.
- ItemsViewController2 changes shared Items2 layout hooks; include CarouselView.
- No Android, Windows, or unrelated UI coverage.
MAI Code 2 — iOS and Mac Catalyst CollectionView2 scroll-anchor fix; runs CollectionView and CarouselView coverage on both platforms.
CollectionViewScrollAnchorandItemsViewController2change the iOS Items2 layout/animation handling, which is shared by UIKit and Mac Catalyst.- Items2 affects both CollectionView and CarouselView behavior, so both UI categories are needed on iOS and Mac Catalyst.
- The changed device test directly validates CollectionView ScrollAnchorMode end-following behavior on iOS/Mac Catalyst; Android and Windows are unaffected.
- Narrow selection avoids unrelated controls and a platform-wide/full run because the change is confined to the UIKit Items2 scroll anchor.
Device tests
Each model chose independently; every test that runs was selected by at least one of them.
| Platform | GPT-6.2 Sol | MAI Code 2 |
|---|---|---|
| Android | — | — |
| iOS | Controls.CollectionView, Controls.CarouselView |
Controls.CollectionView, Controls.CarouselView |
| MacCatalyst | Controls.CollectionView, Controls.CarouselView |
Controls.CollectionView, Controls.CarouselView |
| Windows | — | — |
GPT-6.2 Sol — Run CollectionView and CarouselView device tests on iOS and Mac Catalyst, including the new scroll-anchor regression.
- CollectionViewTests.ScrollAnchor adds an iOS/Mac Catalyst regression for short rows and end anchoring.
- Cover CarouselView for the shared Items2 controller hooks.
- No Core handler or other platform changes.
MAI Code 2 — Run CollectionView and coupled CarouselView device tests on iOS and Mac Catalyst for the Items2 anchor-animation fix.
- The fix is in the iOS Items2 handler path and also compiles for Mac Catalyst.
- The changed CollectionView device test covers the newly handled UIKit layout-started scroll animation.
- Items2 backs both CollectionView and CarouselView, so both Controls device units are coupled to the changed anchor logic.
- No Android, Windows, Core, Graphics, or Essentials device tests are needed because none use this iOS-specific code path.
Generated by /review run-tests. A targeted run is a fast signal, not full-suite certification.
There was a problem hiding this comment.
Expert Review — 1 finding · ⚠️ 1 breaking change
Reviewed commit 06dd7c4.
Warning
Existing apps would look or behave differently without opting in. See the Behavioral Change Disclosure findings at:
See inline comments for details.
| if (!_animatingBeforeLayout && isVertical && _tracker.IsAnchored && _edgeAnimation is null && !_animatingRequest | ||
| && !collectionView.Dragging && !collectionView.Decelerating && IsScrollAnimating(collectionView)) | ||
| { | ||
| ApplyOffset(collectionView, collectionView.ContentOffset.Y); |
There was a problem hiding this comment.
[major] Behavioral Change Disclosure - This also stops deliberate native animations in existing controller customizations. On iOS/MacCatalyst 17.4+, a custom controller supplied through CollectionViewHandler2.CreateController can call base.ViewWillLayoutSubviews() and then CollectionView.SetContentOffset(target, true) while a vertical list follows its end, with fully measured, unchanged content and an in-bounds target 100 points above the end. Before, the native animation could reach its target and release anchoring; now this branch cancels it before arrival. LayoutStarting recorded no animation, native scrolling sets neither request flag, and Scrolled defers tracking while layout depth is positive, so all cancellation conditions match without any content shrinkage. There is no opt-in/opt-out preserving both anchoring and this native customization; AnchorMode=None disables anchoring altogether. Distinguish an automatic shrink/out-of-bounds adjustment from a deliberate native animation before cancelling it. Add a regression test starting the deliberate animation between layout start and finish: the existing native-animation test starts before layout, while the new short-row test exercises only automatic correction.

Agent assessment
Self-reported assessment, not an independent review.
Assessed revision:
06dd7c47b0c2d9b0c41cb5aafc4e57fc09571b75↑ Whole CollectionView device-test family with the fix: Mac Catalyst 422 passed, 0 failed, 8 skipped in two runs; iOS 27 418 passed, 0 failed, 8 skipped.
↑ Acts only on a scroll animation UIKit starts inside a layout pass while the view follows an edge; animations the app or the anchor started, drags and decelerations are excluded. No public API change. Merges cleanly with #40828.
↓ No PR CI: the device-test pipelines are disabled. The local host runs macOS 26.7.3; CI runs macOS 27.2.
↑ Anchor trace: ScrollAnimating turns true in that layout pass, the anchor counts it as user scrolling, and it switches from End to None once the re-targeted animation is 10 points above the end.
↓ When UIKit animates this adjustment rather than applying it at once is inferred from private method names.
↓ The flaky test's own shrink step doesn't start this animation on macOS 26.7.3 or the iOS 27 simulator (100+ local runs pass before and after the fix), so it isn't confirmed that macOS 27 takes this path; CI is disabled.
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Target Branch
mainnet11, for reviewFor .NET 11 fixes, only a maintainer retargets the approved PR to the active
inflight lane and merges it after CI validates that final base. Do not merge
individual fixes directly into
net11; agents must not retarget or merge.Description of Change
This investigates the intermittent Mac Catalyst device-test failure
[CollectionView] Back at the end after a ScrollTo away, the list keeps the end in view as the last item shrinksonmain("Following the end, the list should keep the end in view as the last item shrinks. Distance to the end: 370."). It fixes a deterministicScrollAnchorMode(#40211) defect on iOS and Mac Catalyst that leaves the list in the same failed state.Root cause. When the content of a
UICollectionViewshrinks under its content offset during a layout pass, UIKit animates the view back within the content. Native stack captured locally on Mac Catalyst:CollectionViewScrollAnchortreats every scroll animation it didn't start as a fling (ScrollAnimating), and stops correcting until it ends. On its way, UIKit measures the rows the animation brings into view and re-targets it, so the view ends up above where the end settles. The anchor sees the view move more than its threshold away from the end and stops following it (End → None), and the view stays there.This happens whenever rows are shorter than UIKit's 30-point estimate (
LayoutFactory2usesNSCollectionLayoutDimension.CreateEstimated(30)). The anchor goes to the end as estimated, UIKit measures the rows there shorter, the content shrinks under the view, and UIKit animates back.Fix.
CollectionViewScrollAnchor.LayoutStartingnow notes whether a scroll animation is running when an outermost layout pass starts. InLayoutFinished, if one is running that wasn't before, and neither the anchor nor the app started one (no edge animation, no animated request, no drag or deceleration), UIKit started it while laying the view out. A view that follows an edge stops it where it starts withSetContentOffset(ContentOffset, false), the idiom the controller already uses to stop animations. The usual post-layout look then scrolls to the edge. Views that don't follow an edge keep UIKit's animation. The threeLayoutStartingcall sites inItemsViewController2now pass the collection view.Regression test.
AnchorMode End shows and follows the end of rows shorter than UIKit estimates them(iOS and Mac Catalyst): 40 rows of 20 points must open at their end, and the list must keep following its end when 20 more arrive.OnLoadedAsync) before the test body ranThe repeated runs used a temporary local theory that called the new test; it isn't part of this PR. The flaky test also passed 40/40 locally with the fix, and its assertions are unchanged.
Relationship to the Mac Catalyst CI failure. Both observed failures end with the list no longer following its end and a stable 370 points above it: build 1704369 on main
9c4919b856([NavigationViewHandler]) and build 1704494 ([NavigationRenderer]). That's the state this defect leaves the view in. Locally, though, the flaky test's shrink step doesn't take this path. On macOS 26.7.3 and on the iOS 27 simulator, UIKit re-estimates the rows above and moves the offset in that layout pass before it shrinks the content, so it never animates. More than 100 local runs of that test passed, with and without this change, including in the full family. CI runs macOS 27.2, which I couldn't run locally. So this PR fixes a real, deterministic anchor defect with the same failure signature, and it's the most likely explanation I found, but it doesn't prove that the macOS 27 run took this path.Other findings, not changed here:
preferredFocusedView. UIKit then sizes that cell outside a layout pass (_endReusableViewCreationAndSelfSizingOutsideVisibleCellsUpdate), and applies the compositional layout's content-offset adjustment at once. It reports the move toscrollViewDidScrollbefore it updates the content size, and the anchor judges it as a scroll. With rows taller than the estimate, as in the flaky test, the move is toward the end and harmless. With shorter rows it briefly un-anchors the list. A possible follow-up is to treat offset changes made insideCustomUICollectionViewCompositionalLayout.InvalidateLayout(context)as layout.ScrollToStart/ScrollToEndbranches; this PR changesLayoutStarting/LayoutFinished. Their hunks don't overlap, and a trial merge is clean. [iOS/MacCatalyst] Preserve newer CollectionView scroll completion during reentrant scrolls #40817 is unaffected.CI.
maui-pr,maui-pr-uitestsandmaui-pr-devicetestshave been disabled since 2026-11-29 23:22 UTC, so CI hasn't run on this PR. It's pending until the pipelines are re-enabled; the validation above is local.Validation, with the pinned SDK
12.0.100-alpha.1.26523.103and Xcode 27.2 (hence-SkipXcodeVersionCheck):Issues Fixed
No standalone issue. Related to #40211 (ScrollAnchorMode), the intermittent Mac Catalyst failure above, #40828 and #40817.
Let AI finish this PR