Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 20 additions & 25 deletions src/Core/src/Platform/iOS/MauiScrollView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// 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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -817,8 +813,8 @@ bool IPlatformMeasureInvalidationController.InvalidateMeasure(bool isPropagating
}

/// <summary>
    /// Called when the scroll orientation has changed to trigger proper RTL layout recalculation.
    /// </summary>
/// Called when the scroll orientation has changed to trigger proper RTL layout recalculation.
/// </summary>

internal void OnOrientationChanged()
{
Expand Down Expand Up @@ -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;
Expand Down
178 changes: 178 additions & 0 deletions src/Core/tests/DeviceTests/Platform/iOS/MauiScrollViewTests.iOS.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Loading