From 31b74ed3ae417203c1df0c6334b5b946e6c6fd51 Mon Sep 17 00:00:00 2001 From: Dhivya-SF4094 <127717131+Dhivya-SF4094@users.noreply.github.com> Date: Fri, 1 Aug 2025 19:29:37 +0530 Subject: [PATCH 1/2] Fixed CollectionView does not update layout correctly when ItemsSource changes --- .../Handlers/Items2/ItemsViewHandler2.iOS.cs | 12 +++- .../TestCases.HostApp/Issues/Issue30953.cs | 70 +++++++++++++++++++ .../Tests/Issues/Issue30953.cs | 22 ++++++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/Controls/tests/TestCases.HostApp/Issues/Issue30953.cs create mode 100644 src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue30953.cs diff --git a/src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs b/src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs index 87cfb828150a5..53034afe4fb4e 100644 --- a/src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs +++ b/src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs @@ -182,7 +182,17 @@ protected bool IsIndexPathValid(NSIndexPath indexPath) public override Size GetDesiredSize(double widthConstraint, double heightConstraint) { - var contentSize = Controller.GetSize(); + var potentialContentSize = Controller.GetSize(); + + // If contentSize comes back null, it means none of the content has been realized yet; + // we need to return the expansive size the collection view wants by default to get + // it to start measuring its content + if (potentialContentSize.Height == 0 || potentialContentSize.Width == 0) + { + return base.GetDesiredSize(widthConstraint, heightConstraint); + } + + var contentSize = potentialContentSize; // Our target size is the smaller of it and the constraints var width = contentSize.Width <= widthConstraint ? contentSize.Width : widthConstraint; diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue30953.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue30953.cs new file mode 100644 index 0000000000000..a1c8f8406c38d --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue30953.cs @@ -0,0 +1,70 @@ +using System.Collections.ObjectModel; + +namespace Maui.Controls.Sample.Issues; + +[Issue(IssueTracker.Github, 30953, "CollectionView does not update layout correctly when ItemsSource changes", PlatformAffected.iOS | PlatformAffected.macOS)] +public class Issue30953 : ContentPage +{ + private ObservableCollection Countries = new(); + + public Issue30953() + { + LoadCountries(); + var collectionView = new CollectionView2(); + var button = new Button + { + Text = "Set ItemsSource", + AutomationId = "Issue30953Button", + HorizontalOptions = LayoutOptions.Center + }; + + button.Clicked += (sender, args) => + { + collectionView.ItemsSource = Countries; + }; + + var label = new Label + { + Text = "The test passed if the CollectionView ItemsSource is set and the items are displayed correctly in runtime.", + HorizontalOptions = LayoutOptions.Center + }; + + var stack = new VerticalStackLayout + { + Children = { + label, + button, + collectionView + } + }; + + Content = stack; + } + + void LoadCountries() + { + Countries = new ObservableCollection + { + "United States", + "Canada", + "United Kingdom", + "Germany", + "France", + "Italy", + "Spain", + "Japan", + "Australia", + "Brazil", + "India", + "China", + "Russia", + "Mexico", + "Argentina", + "South Africa", + "Egypt", + "Turkey", + "Netherlands", + "Sweden" + }; + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue30953.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue30953.cs new file mode 100644 index 0000000000000..0ded46d1140ec --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue30953.cs @@ -0,0 +1,22 @@ +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues; + +public class Issue30953 : _IssuesUITest +{ + public Issue30953(TestDevice testDevice) : base(testDevice) + { + } + public override string Issue => "CollectionView does not update layout correctly when ItemsSource changes"; + + [Test] + [Category(UITestCategories.CollectionView)] + public void EnsureCollectionViewLayoutOnItemsSourceChange() + { + App.WaitForElement("Issue30953Button"); + App.Tap("Issue30953Button"); + App.WaitForElement("United States"); + } +} \ No newline at end of file From ad99847fd10b88a27e951f18cd98673b179924dc Mon Sep 17 00:00:00 2001 From: Dhivya-SF4094 <127717131+Dhivya-SF4094@users.noreply.github.com> Date: Fri, 1 Aug 2025 20:03:46 +0530 Subject: [PATCH 2/2] Optimized fix --- .../src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs b/src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs index 53034afe4fb4e..91fcadc6ad149 100644 --- a/src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs +++ b/src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs @@ -182,18 +182,16 @@ protected bool IsIndexPathValid(NSIndexPath indexPath) public override Size GetDesiredSize(double widthConstraint, double heightConstraint) { - var potentialContentSize = Controller.GetSize(); + var contentSize = Controller.GetSize(); // If contentSize comes back null, it means none of the content has been realized yet; // we need to return the expansive size the collection view wants by default to get // it to start measuring its content - if (potentialContentSize.Height == 0 || potentialContentSize.Width == 0) + if (contentSize.Height == 0 || contentSize.Width == 0) { return base.GetDesiredSize(widthConstraint, heightConstraint); } - var contentSize = potentialContentSize; - // Our target size is the smaller of it and the constraints var width = contentSize.Width <= widthConstraint ? contentSize.Width : widthConstraint; var height = contentSize.Height <= heightConstraint ? contentSize.Height : heightConstraint;