diff --git a/src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs b/src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs index c8e8ed19df3d..b56dcd9f4ef9 100644 --- a/src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs +++ b/src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs @@ -146,7 +146,8 @@ protected virtual void ScrollToRequested(object sender, ScrollToRequestEventArgs return; } - var position = Items.ScrollToPositionExtensions.ToCollectionViewScrollPosition(args.ScrollToPosition, UICollectionViewScrollDirection.Vertical); + var scrollDirection = Controller.GetScrollDirection(); + var position = Items.ScrollToPositionExtensions.ToCollectionViewScrollPosition(args.ScrollToPosition, scrollDirection); Controller.CollectionView.ScrollToItem(indexPath, position, args.IsAnimated); diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue33852.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue33852.cs new file mode 100644 index 000000000000..904f7cbc2513 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue33852.cs @@ -0,0 +1,85 @@ +using System.Collections.ObjectModel; + +namespace Maui.Controls.Sample.Issues; + +[Issue(IssueTracker.Github, 33852, "CollectionView ScrollTo does not work with horizontal layout", PlatformAffected.iOS | PlatformAffected.macOS)] +public class Issue33852 : ContentPage +{ + public ObservableCollection Items { get; set; } + private Label _indexLabel; + private CollectionView2 _collectionView; + public Issue33852() + { + Items = new ObservableCollection(); + for (int i = 0; i <= 50; i++) + { + Items.Add($"Item_{i}"); + } + + _indexLabel = new Label + { + AutomationId = "IndexLabel", + Text = "ItemIndex: 0" + }; + + var scrollToButton = new Button + { + AutomationId = "ScrollToButton", + Text = "Scroll to Item 15", + WidthRequest = 150 + }; + scrollToButton.Clicked += OnScrollToButtonClicked; + + _collectionView = new CollectionView2 + { + AutomationId = "TestCollectionView", + ItemsSource = Items, + HeightRequest = 600, + ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal) + { + ItemSpacing = 10 + }, + ItemTemplate = new DataTemplate(() => + { + var label = new Label(); + label.SetBinding(Label.TextProperty, "."); + return new Border + { + Margin = new Thickness(5), + Padding = new Thickness(10), + Stroke = Colors.Gray, + Content = label + }; + }) + }; + + _collectionView.Scrolled += OnCollectionViewScrolled; + + Content = new StackLayout + { + Children = + { + _indexLabel, + scrollToButton, + _collectionView + } + }; + } + + private void OnCollectionViewScrolled(object sender, ItemsViewScrolledEventArgs e) + { + if (e.FirstVisibleItemIndex > 0) + { + _indexLabel.Text = $"The CollectionView is scrolled"; + } + else + { + _indexLabel.Text = "The CollectionView does not scroll"; + } + } + + private void OnScrollToButtonClicked(object sender, EventArgs e) + { + _collectionView.ScrollTo(15, position: ScrollToPosition.Start, animate: true); + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue33852.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue33852.cs new file mode 100644 index 000000000000..3adea182a2a1 --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue33852.cs @@ -0,0 +1,24 @@ +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues; + +public class Issue33852 : _IssuesUITest +{ + public Issue33852(TestDevice testDevice) : base(testDevice) + { + } + + public override string Issue => "CollectionView ScrollTo does not work with horizontal layout"; + + [Test] + [Category(UITestCategories.CollectionView)] + public void ProgrammaticScrollToWorksWithHorizontalLayout() + { + App.WaitForElement("ScrollToButton"); + App.Tap("ScrollToButton"); + var firstIndexText = App.FindElement("IndexLabel").GetText(); + Assert.That(firstIndexText, Is.EqualTo("The CollectionView is scrolled")); + } +} \ No newline at end of file