diff --git a/src/Controls/src/Core/Handlers/Items2/iOS/LayoutFactory2.cs b/src/Controls/src/Core/Handlers/Items2/iOS/LayoutFactory2.cs index 311d660d7249..e06ab037b99a 100644 --- a/src/Controls/src/Core/Handlers/Items2/iOS/LayoutFactory2.cs +++ b/src/Controls/src/Core/Handlers/Items2/iOS/LayoutFactory2.cs @@ -503,6 +503,7 @@ class CustomUICollectionViewCompositionalLayout : UICollectionViewCompositionalL ItemsLayout? _itemsLayout; LayoutGroupingInfo? _groupingInfo; LayoutHeaderFooterInfo? _headerFooterInfo; + CGSize _currentSize; public CustomUICollectionViewCompositionalLayout(LayoutSnapInfo snapInfo, LayoutGroupingInfo? groupingInfo, LayoutHeaderFooterInfo? headerFooterInfo, UICollectionViewCompositionalLayoutSectionProvider sectionProvider, UICollectionViewCompositionalLayoutConfiguration configuration, ItemsLayout? itemsLayout) : base(sectionProvider, configuration) { @@ -555,6 +556,20 @@ void ForceScrollToLastItem(UICollectionView collectionView) } } + public override bool ShouldInvalidateLayoutForBoundsChange(CGRect newBounds) + { + // If the size hasn't changed, use the base implementation + if (newBounds.Size.IsCloseTo(_currentSize)) + { + return base.ShouldInvalidateLayoutForBoundsChange(newBounds); + } + + // Size has changed (e.g., rotation), so we need to invalidate the layout + // to ensure cells are properly measured and displayed + _currentSize = newBounds.Size; + return true; + } + public override CGPoint TargetContentOffset(CGPoint proposedContentOffset, CGPoint scrollingVelocity) { var snapPointsType = _snapInfo.SnapType; diff --git a/src/Controls/tests/TestCases.Android.Tests/snapshots/android/VerifyCollectionViewTextShouldAppearAfterRotatingTheDevice.png b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/VerifyCollectionViewTextShouldAppearAfterRotatingTheDevice.png new file mode 100644 index 000000000000..e0ca9a3a7b88 Binary files /dev/null and b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/VerifyCollectionViewTextShouldAppearAfterRotatingTheDevice.png differ diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue32435.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue32435.cs new file mode 100644 index 000000000000..4acfa08427b5 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue32435.cs @@ -0,0 +1,74 @@ +using Microsoft.Maui.Controls; +using System.Collections.ObjectModel; + +namespace Maui.Controls.Sample.Issues; + +[Issue(IssueTracker.Github, 32435, "Rotating the Simulator causes the text on the collection view to disappear", PlatformAffected.iOS)] + +public class Issue32435 : ContentPage +{ + readonly ObservableCollection items = new(); + readonly CollectionView2 collectionView; + + public Issue32435() + { + var rootGrid = new Grid + { + Margin = 20, + RowDefinitions = + { + new RowDefinition { Height = GridLength.Auto }, + new RowDefinition { Height = GridLength.Auto }, + new RowDefinition { Height = GridLength.Star } + } + }; + + var topStack = new StackLayout(); + topStack.Add(new Label { Text = "CollectionView text should appear after rotating the device", AutomationId = "InstructionLabel" }); + rootGrid.Add(topStack); + Grid.SetRow(topStack, 0); + + var addButton = new Button + { + Text = "Add", + AutomationId = "AddButton", + HorizontalOptions = LayoutOptions.Start, + VerticalOptions = LayoutOptions.Start + }; + addButton.Clicked += ButtonAdd_Clicked; + rootGrid.Add(addButton); + Grid.SetRow(addButton, 1); + + var innerGrid = new Grid + { + RowDefinitions = + { + new RowDefinition { Height = GridLength.Auto }, + new RowDefinition { Height = GridLength.Star } + } + }; + + collectionView = new CollectionView2 + { + BackgroundColor = Colors.LightSalmon, + HeightRequest = 50, + ItemsLayout = new GridItemsLayout(ItemsLayoutOrientation.Horizontal) + }; + + innerGrid.Add(collectionView); + Grid.SetRow(collectionView, 0); + + rootGrid.Add(innerGrid); + Grid.SetRow(innerGrid, 2); + + items.Add("item: " + items.Count); + collectionView.ItemsSource = items; + + Content = rootGrid; + } + + void ButtonAdd_Clicked(object sender, System.EventArgs e) + { + items.Add("item: " + items.Count); + } +} diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue32435.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue32435.cs new file mode 100644 index 000000000000..c51fa8d092b1 --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue32435.cs @@ -0,0 +1,24 @@ +#if TEST_FAILS_ON_CATALYST && TEST_FAILS_ON_WINDOWS //Issue reproduce only when rotating device. +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues; + +public class Issue32435 : _IssuesUITest +{ + public Issue32435(TestDevice device) : base(device) { } + + public override string Issue => "Rotating the Simulator causes the text on the collection view to disappear"; + [Test] + [Category(UITestCategories.CollectionView)] + public void VerifyCollectionViewTextShouldAppearAfterRotatingTheDevice() + { + App.WaitForElement("InstructionLabel"); + App.Tap("AddButton"); + App.SetOrientationLandscape(); + App.SetOrientationPortrait(); + VerifyScreenshot(); + } +} +#endif \ No newline at end of file diff --git a/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios-26/VerifyCollectionViewTextShouldAppearAfterRotatingTheDevice.png b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios-26/VerifyCollectionViewTextShouldAppearAfterRotatingTheDevice.png new file mode 100644 index 000000000000..4ce298e125e6 Binary files /dev/null and b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios-26/VerifyCollectionViewTextShouldAppearAfterRotatingTheDevice.png differ diff --git a/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/VerifyCollectionViewTextShouldAppearAfterRotatingTheDevice.png b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/VerifyCollectionViewTextShouldAppearAfterRotatingTheDevice.png new file mode 100644 index 000000000000..8192e595290a Binary files /dev/null and b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/VerifyCollectionViewTextShouldAppearAfterRotatingTheDevice.png differ