diff --git a/src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs b/src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs index 87cfb828150a..91fcadc6ad14 100644 --- a/src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs +++ b/src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs @@ -184,6 +184,14 @@ public override Size GetDesiredSize(double widthConstraint, double heightConstra { 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 (contentSize.Height == 0 || contentSize.Width == 0) + { + return base.GetDesiredSize(widthConstraint, heightConstraint); + } + // 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; 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 000000000000..a1c8f8406c38 --- /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 000000000000..0ded46d1140e --- /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