Repository navigation
[iOS] Fix for CollectionView.Measure() returning incorrect height when called before the view is mounted #34331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
src/Controls/tests/TestCases.HostApp/Issues/Issue32983.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| using System.Collections.ObjectModel; | ||
| #if IOS || MACCATALYST | ||
| using Microsoft.Maui.Platform; | ||
| using UIKit; | ||
| #endif | ||
|
|
||
| namespace Maui.Controls.Sample.Issues; | ||
|
|
||
| [Issue(IssueTracker.Github, 32983, "CollectionView messes up Measure operation on Views", PlatformAffected.iOS)] | ||
| public class Issue32983 : ContentPage | ||
| { | ||
| readonly Label _measuredHeightLabel; | ||
| readonly Label _statusLabel; | ||
| readonly VerticalStackLayout _rootLayout; | ||
|
|
||
| public Issue32983() | ||
| { | ||
| _measuredHeightLabel = new Label | ||
| { | ||
| AutomationId = "MeasuredHeight", | ||
| Text = "Pending" | ||
| }; | ||
|
|
||
| _statusLabel = new Label | ||
| { | ||
| AutomationId = "StatusLabel", | ||
| Text = "Tap the button to reproduce the issue" | ||
| }; | ||
|
|
||
| var showButton = new Button | ||
| { | ||
| Text = "ShowBottomSheet", | ||
| AutomationId = "ShowBottomSheetButton", | ||
| HorizontalOptions = LayoutOptions.Fill | ||
| }; | ||
| showButton.Clicked += OnShowBottomSheetClicked; | ||
|
|
||
| _rootLayout = new VerticalStackLayout | ||
| { | ||
| Padding = 10, | ||
| Spacing = 8, | ||
| Children = | ||
| { | ||
| showButton, | ||
| _statusLabel, | ||
| new Label { Text = "Measured height (ContentView unmounted):" }, | ||
| _measuredHeightLabel | ||
| } | ||
| }; | ||
|
|
||
| Content = _rootLayout; | ||
| } | ||
|
|
||
| void OnShowBottomSheetClicked(object sender, EventArgs e) | ||
| { | ||
| #if IOS || MACCATALYST | ||
| var bottomSheetContent = new Issue32983BottomSheetContentView(); | ||
| var vm = new Issue32983ViewModel(); | ||
| for (int i = 0; i < 10; i++) | ||
| { | ||
| vm.ListOfStuff.Add(new Issue32983Item("Item #" + (i + 1))); | ||
| } | ||
| bottomSheetContent.BindingContext = vm; | ||
|
|
||
| var mauiContext = this.Handler?.MauiContext ?? throw new Exception("MauiContext is null"); | ||
| var parent = this.ToUIViewController(mauiContext); | ||
| var viewControllerToPresent = bottomSheetContent.ToUIViewController(mauiContext); | ||
|
|
||
| var display = DeviceDisplay.MainDisplayInfo; | ||
| double widthDip = display.Width / display.Density; | ||
| double heightDip = display.Height / display.Density; | ||
|
|
||
| var minimumSize = bottomSheetContent.Measure(widthDip, heightDip); | ||
| var detentSize = minimumSize.ToCGSize(); | ||
|
|
||
| if (OperatingSystem.IsIOSVersionAtLeast(16) || OperatingSystem.IsMacCatalystVersionAtLeast(16)) | ||
| { | ||
| var detent = UISheetPresentationControllerDetent.Create( | ||
| "ContentHeight", | ||
| (ctx) => (nfloat)detentSize.Height); | ||
|
|
||
| var sheet = viewControllerToPresent.SheetPresentationController; | ||
| if (sheet is not null) | ||
| { | ||
| sheet.Detents = new[] { detent }; | ||
| sheet.LargestUndimmedDetentIdentifier = UISheetPresentationControllerDetentIdentifier.Unknown; | ||
| sheet.PrefersScrollingExpandsWhenScrolledToEdge = false; | ||
| sheet.PrefersEdgeAttachedInCompactHeight = true; | ||
| sheet.WidthFollowsPreferredContentSizeWhenEdgeAttached = true; | ||
| } | ||
| } | ||
| parent.PresentViewController(viewControllerToPresent, animated: true, completionHandler: null); | ||
| #endif | ||
| } | ||
| } | ||
|
|
||
| public class Issue32983BottomSheetContentView : Microsoft.Maui.Controls.ContentView | ||
| { | ||
| public Issue32983BottomSheetContentView() | ||
| { | ||
| BackgroundColor = Colors.AliceBlue; | ||
|
|
||
| var collectionView = new CollectionView | ||
| { | ||
| SelectionMode = SelectionMode.Single, | ||
| VerticalOptions = LayoutOptions.Start, | ||
| Margin = new Thickness(10), | ||
| AutomationId = "BottomSheetCollectionView", | ||
|
|
||
| ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Vertical), | ||
|
|
||
| ItemTemplate = new DataTemplate(() => | ||
| { | ||
| var label = new Label | ||
| { | ||
| FontSize = 20, | ||
| FontAttributes = FontAttributes.Bold | ||
| }; | ||
|
|
||
| label.SetBinding(Label.TextProperty, nameof(Issue32983Item.Name)); | ||
| label.SetBinding(Label.AutomationIdProperty, nameof(Issue32983Item.Name)); | ||
|
|
||
| return label; | ||
| }) | ||
| }; | ||
|
|
||
| collectionView.SetBinding(ItemsView.ItemsSourceProperty, nameof(Issue32983ViewModel.ListOfStuff)); | ||
|
|
||
| Content = collectionView; | ||
| } | ||
| } | ||
|
|
||
| public class Issue32983Item(string name) | ||
| { | ||
| public string Name => name; | ||
| } | ||
|
|
||
| public class Issue32983ViewModel | ||
| { | ||
| public ObservableCollection<Issue32983Item> ListOfStuff { get; } = new(); | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue32983.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // This test is iOS-only: The test sample uses UISheetPresentationController with a custom detent to present a bottom sheet sized to the measured content height. This API is not available on Android or Windows, and on MacCatalyst it ignores custom detents and always presents as a full-height modal. | ||
| #if IOS | ||
| using NUnit.Framework; | ||
| using UITest.Appium; | ||
| using UITest.Core; | ||
|
|
||
| namespace Microsoft.Maui.TestCases.Tests.Issues; | ||
|
|
||
| public class Issue32983 : _IssuesUITest | ||
| { | ||
| public override string Issue => "CollectionView messes up Measure operation on Views"; | ||
|
|
||
| public Issue32983(TestDevice device) : base(device) { } | ||
|
|
||
| [Test] | ||
| [Category(UITestCategories.CollectionView)] | ||
| public void BottomSheetDetentHeightIsCorrectWhenCollectionViewIsMeasuredBeforeMount() | ||
| { | ||
| App.WaitForElement("ShowBottomSheetButton"); | ||
| App.Tap("ShowBottomSheetButton"); | ||
|
|
||
| App.WaitForElement("BottomSheetCollectionView"); | ||
| VerifyScreenshot(); | ||
| } | ||
| } | ||
| #endif | ||
Binary file added
BIN
+111 KB
...-26/BottomSheetDetentHeightIsCorrectWhenCollectionViewIsMeasuredBeforeMount.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+82.8 KB
...ios/BottomSheetDetentHeightIsCorrectWhenCollectionViewIsMeasuredBeforeMount.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.