diff --git a/src/Controls/src/Core/Handlers/Items2/iOS/LayoutFactory2.cs b/src/Controls/src/Core/Handlers/Items2/iOS/LayoutFactory2.cs index 4f9df4314b92..e0adc6cb4c39 100644 --- a/src/Controls/src/Core/Handlers/Items2/iOS/LayoutFactory2.cs +++ b/src/Controls/src/Core/Handlers/Items2/iOS/LayoutFactory2.cs @@ -343,7 +343,17 @@ public static UICollectionViewLayout CreateCarouselLayout( return; } - var page = (offset.X + sectionMargin) / (env.Container.ContentSize.Width - sectionMargin * 2); + // Calculate page index accounting for ItemSpacing + var itemSpacing = itemsView.ItemsLayout is LinearItemsLayout linearLayout ? linearLayout.ItemSpacing : 0; + + var effectiveItemWidth = env.Container.ContentSize.Width - sectionMargin * 2 + itemSpacing; + + if (effectiveItemWidth <= 0) + { + return; + } + + double page = (offset.X + sectionMargin) / effectiveItemWidth; if (Math.Abs(page % 1) > (double.Epsilon * 100) || cv2Controller.ItemsSource.ItemCount <= 0) { diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue32048.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue32048.cs new file mode 100644 index 000000000000..d76183e5c825 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue32048.cs @@ -0,0 +1,78 @@ +namespace Maui.Controls.Sample.Issues; + +[Issue(IssueTracker.Github, 32048, "CurrentItem does not update when ItemSpacing is set", PlatformAffected.iOS)] +public class Issue32048 : ContentPage +{ + CarouselView2 carouselView; + Label currentItemLabel; + string firstItem = "Baboon"; + + public Issue32048() + { + carouselView = new CarouselView2 + { + AutomationId = "CarouselViewWithItemSpacing", + HeightRequest = 400, + BackgroundColor = Colors.LightGray, + ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal) + { + ItemSpacing = 10, + SnapPointsType = SnapPointsType.MandatorySingle, + }, + ItemTemplate = new DataTemplate(() => + { + Label label = new Label + { + HorizontalOptions = LayoutOptions.Center, + VerticalOptions = LayoutOptions.Center + }; + label.SetBinding(Label.TextProperty, "."); + + return new Grid + { + Children = { label } + }; + }), + ItemsSource = new string[] + { + "Baboon", + "Capuchin Monkey", + "Blue Monkey", + "Squirrel Monkey", + "Golden Lion Tamarin" + } + }; + + carouselView.CurrentItemChanged += OnCurrentItemChanged; + + currentItemLabel = new Label + { + AutomationId = "Issue32048StatusLabel", + Text = "Failure" + }; + + Grid grid = new Grid + { + Padding = 25, + RowSpacing = 10, + RowDefinitions = + { + new RowDefinition(GridLength.Auto), + new RowDefinition(GridLength.Auto) + } + }; + + grid.Add(carouselView); + grid.Add(currentItemLabel, row: 1); + + Content = grid; + } + + void OnCurrentItemChanged(object sender, CurrentItemChangedEventArgs e) + { + if (e.CurrentItem is not null && e.CurrentItem.ToString() != firstItem) + { + currentItemLabel.Text = "Success"; + } + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue32048.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue32048.cs new file mode 100644 index 000000000000..4c2f629aa245 --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue32048.cs @@ -0,0 +1,30 @@ +#if TEST_FAILS_ON_WINDOWS // Issue Link - https://github.com/dotnet/maui/issues/31670 +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues; + +public class Issue32048 : _IssuesUITest +{ + public Issue32048(TestDevice device) : base(device) + { + } + + public override string Issue => "CurrentItem does not update when ItemSpacing is set"; + + [Test] + [Category(UITestCategories.CarouselView)] + public void VerifyCurrentItemUpdatesWithItemSpacing() + { + App.WaitForElement("CarouselViewWithItemSpacing"); + App.ScrollRight("CarouselViewWithItemSpacing"); + +#if MACCATALYST + Thread.Sleep(1000); +#endif + var resultLabel = App.WaitForElement("Issue32048StatusLabel").GetText(); + Assert.That(resultLabel, Is.EqualTo("Success")); + } +} +#endif \ No newline at end of file