diff --git a/src/Core/src/Platform/iOS/MauiScrollView.cs b/src/Core/src/Platform/iOS/MauiScrollView.cs index 9a03fede320a..116938bdf7ed 100644 --- a/src/Core/src/Platform/iOS/MauiScrollView.cs +++ b/src/Core/src/Platform/iOS/MauiScrollView.cs @@ -78,10 +78,6 @@ public class MauiScrollView : UIScrollView, IUIViewLifeCycleEvents, ICrossPlatfo readonly bool[] _blockedEdgesCache = new bool[4]; bool _blockedEdgesCacheValid; - // Cached result of whether this scroll view is hosted by a PageSheet. - // Invalidated when UIKit moves the scroll view to a window. - bool? _isInsidePageSheet; - /// /// Flag indicating whether this scroll view should apply safe area adjustments to its content. /// Only true when not nested in another scroll view, no parent MauiView handles it, and safe area is not empty. @@ -641,21 +637,27 @@ Size CrossPlatformArrange(CGRect bounds) // This logic adds safe area padding to the contentSize *only if* the content is nearly large enough to require scrolling, // ensuring the scroll view remains in "scrollable mode" and keeps safe area insets at the scroll view level. // This avoids inset flip-flopping and keeps layout behavior stable and predictable. - if (ContentInsetAdjustmentBehavior == UIScrollViewContentInsetAdjustmentBehavior.Automatic && !IsInsidePageSheet()) + if (ContentInsetAdjustmentBehavior == UIScrollViewContentInsetAdjustmentBehavior.Automatic) { + var inflateWidth = width <= Bounds.Width && + (_safeArea.HorizontalThickness + width) > Bounds.Width; + var inflateHeight = height <= Bounds.Height && + (_safeArea.VerticalThickness + height) > Bounds.Height; + // We do this to keep the content scrollable // if we don't do this the ContentAdjustedInset + contentSize will cause the content to go off the screen and not be scrollable // So the bottom content will just go off the screen until the contentsize triggers the scrollable area - if (width <= Bounds.Width && - (_safeArea.HorizontalThickness + width) > Bounds.Width) + if ((inflateWidth || inflateHeight) && !IsInsidePageSheet()) { - width += Bounds.Width + 1; - } + if (inflateWidth) + { + width += Bounds.Width + 1; + } - if (height <= Bounds.Height && - (_safeArea.VerticalThickness + height) > Bounds.Height) - { - height = Bounds.Height + 1; + if (inflateHeight) + { + height = Bounds.Height + 1; + } } } else if (SystemAdjustedContentInset == UIEdgeInsets.Zero || ContentInsetAdjustmentBehavior == UIScrollViewContentInsetAdjustmentBehavior.Never) @@ -719,24 +721,18 @@ Size CrossPlatformArrange(CGRect bounds) bool IsInsidePageSheet() { - if (_isInsidePageSheet.HasValue) - { - return _isInsidePageSheet.Value; - } - UIResponder? responder = this; while (responder is not null) { - if (responder is UIViewController { ModalPresentationStyle: UIModalPresentationStyle.PageSheet }) + // A style on an unpresented or child controller does not describe a modal sheet. + if (responder is UIViewController { ParentViewController: null, PresentingViewController: not null } controller) { - _isInsidePageSheet = true; - return true; + return controller.ModalPresentationStyle == UIModalPresentationStyle.PageSheet; } responder = responder.NextResponder; } - _isInsidePageSheet = false; return false; } @@ -817,8 +813,8 @@ bool IPlatformMeasureInvalidationController.InvalidateMeasure(bool isPropagating } /// -     /// Called when the scroll orientation has changed to trigger proper RTL layout recalculation. -     /// + /// Called when the scroll orientation has changed to trigger proper RTL layout recalculation. + /// internal void OnOrientationChanged() { @@ -911,7 +907,6 @@ public override void MovedToWindow() // Clear cached scroll view descendant status since the view hierarchy may have changed _scrollViewDescendant = null; _blockedEdgesCacheValid = false; - _isInsidePageSheet = null; // Mark safe area as invalidated since moving to a new window may change safe area _safeAreaInvalidated = true; diff --git a/src/Core/tests/DeviceTests/Platform/iOS/MauiScrollViewTests.iOS.cs b/src/Core/tests/DeviceTests/Platform/iOS/MauiScrollViewTests.iOS.cs new file mode 100644 index 000000000000..8d8059896ab8 --- /dev/null +++ b/src/Core/tests/DeviceTests/Platform/iOS/MauiScrollViewTests.iOS.cs @@ -0,0 +1,178 @@ +using System; +using System.Threading.Tasks; +using CoreGraphics; +using Microsoft.Maui.DeviceTests.Stubs; +using Microsoft.Maui.Graphics; +using Microsoft.Maui.Platform; +using UIKit; +using Xunit; + +namespace Microsoft.Maui.DeviceTests +{ + [Category(TestCategory.ScrollView)] + public class MauiScrollViewTests : TestBase + { + [Theory] + [InlineData(UIModalPresentationStyle.PageSheet, false, 36, 601)] + [InlineData(UIModalPresentationStyle.PageSheet, true, 36, 580)] + [InlineData(UIModalPresentationStyle.PageSheet, true, 0, 580)] + [InlineData(UIModalPresentationStyle.FullScreen, true, 36, 601)] + [InlineData(UIModalPresentationStyle.FullScreen, false, 36, 601)] + [InlineData(UIModalPresentationStyle.Automatic, false, 36, 601)] + public Task AutomaticInsetStabilizationRequiresPresentedPageSheet( + UIModalPresentationStyle style, bool presented, int adjustedTop, int expectedHeight) => + InvokeOnMainThreadAsync(() => + { + using var presenter = new UIViewController(); + using var controller = new PresentationControllerStub + { + ModalPresentationStyle = style, + Presenter = presented ? presenter : null + }; + var layout = new SafeAreaLayoutStub(); + using var scrollView = CreateScrollView(layout, controller, adjustedTop); + + Arrange(scrollView); + + Assert.Equal(expectedHeight, (double)scrollView.ContentSize.Height); + }); + + [Theory] + [InlineData(UIModalPresentationStyle.FullScreen, 601)] + [InlineData(UIModalPresentationStyle.PageSheet, 580)] + public Task ChildControllerStyleDoesNotOverrideModalPresentation( + UIModalPresentationStyle parentStyle, int expectedHeight) => + InvokeOnMainThreadAsync(() => + { + using var presenter = new UIViewController(); + using var parent = new PresentationControllerStub + { + ModalPresentationStyle = parentStyle, + Presenter = presenter + }; + using var child = new PresentationControllerStub + { + ModalPresentationStyle = UIModalPresentationStyle.PageSheet, + Presenter = presenter + }; + parent.AddChildViewController(child); + var layout = new SafeAreaLayoutStub(); + using var scrollView = CreateScrollView(layout, child, 36); + try + { + Arrange(scrollView); + + Assert.Equal(expectedHeight, (double)scrollView.ContentSize.Height); + } + finally + { + child.RemoveFromParentViewController(); + } + }); + + [Fact] + public Task PresentationChangesAreNotCachedUntilMovedToWindow() => + InvokeOnMainThreadAsync(() => + { + using var presenter = new UIViewController(); + using var controller = new PresentationControllerStub + { + ModalPresentationStyle = UIModalPresentationStyle.PageSheet + }; + var layout = new SafeAreaLayoutStub(); + using var scrollView = CreateScrollView(layout, controller, 36); + + Arrange(scrollView); + Assert.Equal(601, (double)scrollView.ContentSize.Height); + + controller.Presenter = presenter; + Arrange(scrollView); + Assert.Equal(580, (double)scrollView.ContentSize.Height); + + controller.Presenter = null; + Arrange(scrollView); + Assert.Equal(601, (double)scrollView.ContentSize.Height); + }); + + [Theory] + [InlineData(UIModalPresentationStyle.PageSheet, 580)] + [InlineData(UIModalPresentationStyle.FullScreen, 601)] + public Task NativePresentationControlsInsetStabilization(UIModalPresentationStyle style, int expectedHeight) => + InvokeOnMainThreadAsync(async () => + { + var scene = UIApplication.SharedApplication.GetKeyWindow().WindowScene; + using var window = new UIWindow(scene); + using var presenter = new UIViewController(); + using var controller = new UIViewController { ModalPresentationStyle = style }; + window.RootViewController = presenter; + window.Hidden = false; + try + { + await presenter.PresentViewControllerAsync(controller, false); + Assert.NotNull(controller.PresentingViewController); + var layout = new SafeAreaLayoutStub(); + using var scrollView = CreateScrollView(layout, null, 36); + controller.View.AddSubview(scrollView); + try + { + Arrange(scrollView); + + Assert.Equal(expectedHeight, (double)scrollView.ContentSize.Height); + } + finally + { + scrollView.RemoveFromSuperview(); + } + } + finally + { + await presenter.DismissViewControllerAsync(false); + window.Hidden = true; + } + }); + + static InsetScrollView CreateScrollView(SafeAreaLayoutStub layout, UIResponder responder, int adjustedTop) + { + var scrollView = new InsetScrollView + { + Frame = new CGRect(0, 0, 240, 600), + View = layout, + Responder = responder, + SystemInset = new UIEdgeInsets(adjustedTop, 0, 0, 0) + }; + ((ICrossPlatformLayoutBacking)scrollView).CrossPlatformLayout = layout; + return scrollView; + } + + static void Arrange(MauiScrollView scrollView) + { + ((IPlatformMeasureInvalidationController)scrollView).InvalidateMeasure(false); + scrollView.LayoutSubviews(); + } + + class InsetScrollView : MauiScrollView + { + public UIResponder Responder { get; set; } + public UIEdgeInsets SystemInset { get; set; } + public override UIResponder NextResponder => Responder ?? base.NextResponder; + public override UIEdgeInsets SafeAreaInsets => new UIEdgeInsets(36, 0, 0, 0); + public override UIEdgeInsets AdjustedContentInset => SystemInset; + } + + class PresentationControllerStub : UIViewController + { + public UIViewController Presenter { get; set; } + public override UIViewController PresentingViewController => Presenter; + public override UIResponder NextResponder => ParentViewController ?? base.NextResponder; + } + + class SafeAreaLayoutStub : StubBase, ICrossPlatformLayout, ISafeAreaView2 + { + public bool HasExplicitSafeAreaEdges => false; + public Thickness SafeAreaInsets { private get; set; } + public SafeAreaRegions GetSafeAreaRegionsForEdge(int edge) => SafeAreaRegions.Default; + public Size CrossPlatformMeasure(double widthConstraint, double heightConstraint) => new Size(240, 580); + public Size CrossPlatformArrange(Rect bounds) => new Size(240, 580); + } + } +}