Skip to content

[iOS] Support for KeepLastItemInView for CollectionView2 on iOS #31104

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kubaflo
Copy link
Contributor

@kubaflo kubaflo commented Aug 10, 2025

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!

Issues Fixed

Fixes #28716
Fixes #31085

Before After
Screen.Recording.2025-04-01.at.01.17.56.mov
Screen.Recording.2025-04-01.at.01.15.22.mov
<Grid RowDefinitions="Auto, *">
    <Button Text="Add Item"
            Clicked="Button_Clicked"
            HorizontalOptions="Center"
            VerticalOptions="Center"/>
    <CollectionView ItemsUpdatingScrollMode="KeepLastItemInView"
                    Grid.Row="1"
                    x:Name="cv">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Grid ColumnDefinitions="*,Auto">
                    <Label
                        Text="{Binding Text}"
                        HeightRequest="100"
                        VerticalOptions="Center"/>
                </Grid>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</Grid>

@Copilot Copilot AI review requested due to automatic review settings August 10, 2025 10:53
@kubaflo kubaflo requested a review from a team as a code owner August 10, 2025 10:53
@kubaflo kubaflo requested review from mattleibow and rmarinho August 10, 2025 10:53
@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Aug 10, 2025
Copy link
Contributor

Hey there @@kubaflo! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements KeepLastItemInView support for CollectionView on iOS to address scroll behavior when new items are added to the collection. The implementation ensures that when new items are added to a CollectionView, the scroll position automatically moves to keep the last item visible.

Key changes:

  • Added ItemsUpdatingScrollMode property to ItemsLayout to track scroll behavior
  • Modified layout factory methods to pass through the scroll mode configuration
  • Implemented automatic scrolling to last item in CustomUICollectionViewCompositionalLayout

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/Controls/src/Core/Items/ItemsLayout.cs Adds internal ItemsUpdatingScrollMode property to track scroll behavior
src/Controls/src/Core/Handlers/Items2/iOS/LayoutFactory2.cs Updates layout creation methods to accept and handle ItemsUpdatingScrollMode parameter
src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs Maps ItemsUpdatingScrollMode from ItemsView to ItemsLayout
src/Controls/tests/TestCases.HostApp/Issues/Issue28716.xaml Creates test UI with three CollectionViews demonstrating the feature
src/Controls/tests/TestCases.HostApp/Issues/Issue28716.xaml.cs Implements code-behind with data binding and command for adding items
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue28716.cs Adds automated UI test to verify the functionality works correctly


namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 28720, "Support for KeepLastItemInView for CV2", PlatformAffected.iOS)]
Copy link
Preview

Copilot AI Aug 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue number in the attribute (28720) does not match the filename (Issue28716). This should be 28716 to maintain consistency.

Suggested change
[Issue(IssueTracker.Github, 28720, "Support for KeepLastItemInView for CV2", PlatformAffected.iOS)]
[Issue(IssueTracker.Github, 28716, "Support for KeepLastItemInView for CV2", PlatformAffected.iOS)]

Copilot uses AI. Check for mistakes.

}
else
{
collectionView.ScrollToItem(lastIndexPath, UICollectionViewScrollPosition.Right, true);
Copy link
Preview

Copilot AI Aug 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scroll position logic assumes left-to-right layout direction. For right-to-left languages or when FlowDirection is RightToLeft, scrolling to UICollectionViewScrollPosition.Right may not provide the expected behavior of showing the 'last' item from the user's perspective.

Suggested change
collectionView.ScrollToItem(lastIndexPath, UICollectionViewScrollPosition.Right, true);
// Adjust scroll position for RTL layouts
var layoutDirection = collectionView.EffectiveUserInterfaceLayoutDirection;
var scrollPosition = layoutDirection == UIUserInterfaceLayoutDirection.RightToLeft
? UICollectionViewScrollPosition.Left
: UICollectionViewScrollPosition.Right;
collectionView.ScrollToItem(lastIndexPath, scrollPosition, true);

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kubaflo I think this might make sense?

Implements ItemsUpdatingScrollMode.KeepLastItemInView for CollectionView on iOS by updating layout creation and scroll logic. Adds internal ItemsUpdatingScrollMode property to ItemsLayout. Includes new test case and UI test for issue dotnet#28720 to verify correct behavior.
@kubaflo kubaflo force-pushed the cv2-keep-last-item-in-view branch from 2f22a84 to 77c753f Compare August 10, 2025 10:54
@kubaflo kubaflo changed the title Add KeepLastItemInView support for CollectionView on iOS [iOS] Support for KeepLastItemInView for CollectionView2 on iOS Aug 10, 2025
@PureWeen PureWeen added this to the .NET 9 SR11 milestone Aug 11, 2025
@FlavioGoncalves-Cayas
Copy link

Looking forward to test this, but cannot get the artifacts when the build does not finish?

@kubaflo
Copy link
Contributor Author

kubaflo commented Aug 13, 2025

Looking forward to test this, but cannot get the artifacts when the build does not finish?

Yeah we need to trigger a build to generate artifacts. Luckily I know the best /azp triggerer @jfversluis who will do it for us 😄

@jfversluis
Copy link
Member

/azp run MAUI-public

@jfversluis jfversluis moved this from Todo to Ready To Review in MAUI SDK Ongoing Aug 13, 2025
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jfversluis
Copy link
Member

Beep boop 🤖

@FlavioGoncalves-Cayas let us know the results whenever the build finishes, thanks!

@jfversluis jfversluis added the area-controls-collectionview CollectionView, CarouselView, IndicatorView label Aug 13, 2025
@FlavioGoncalves-Cayas
Copy link

There's not artifact produced when it fails like it did right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView collectionview-cv2 community ✨ Community Contribution
Projects
Status: Ready To Review
4 participants