diff --git a/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21726.xaml b/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21726.xaml
new file mode 100644
index 000000000000..9fe10a491ca7
--- /dev/null
+++ b/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21726.xaml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
diff --git a/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21726.xaml.cs b/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21726.xaml.cs
new file mode 100644
index 000000000000..e58a84fd5f1c
--- /dev/null
+++ b/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21726.xaml.cs
@@ -0,0 +1,77 @@
+using System;
+using System.Linq;
+using Microsoft.Maui.Controls;
+using Microsoft.Maui.Controls.Xaml;
+using System.Threading.Tasks;
+
+namespace Maui.Controls.Sample.Issues;
+
+[XamlCompilation(XamlCompilationOptions.Compile)]
+[Issue(IssueTracker.Github, 21726, "Modal with a bottom sheet should not crash iOS Keyboard Scroll", PlatformAffected.iOS)]
+public partial class Issue21726 : ContentPage
+{
+ public Issue21726()
+ {
+ InitializeComponent();
+ }
+
+ void AddVC (object sender, EventArgs e)
+ {
+#if IOS
+ var window = UIKit.UIApplication.SharedApplication
+ .ConnectedScenes
+ .OfType()
+ .SelectMany(s => s.Windows)
+ .FirstOrDefault(w => w.IsKeyWindow);
+
+ var rootVC = window?.RootViewController;
+ while (rootVC?.PresentedViewController != null)
+ {
+ rootVC = rootVC.PresentedViewController;
+ }
+
+ if (rootVC is not null) {
+ var testVC = new TestViewController();
+
+ var testNC = new UIKit.UINavigationController(testVC)
+ {
+ ModalPresentationStyle = UIKit.UIModalPresentationStyle.FullScreen
+ };
+
+ rootVC.PresentViewController(testNC, true, null);
+ }
+#endif
+ }
+
+#if IOS
+ public class TestViewController: UIKit.UIViewController {
+
+ UIKit.UITextField TextField1;
+ UIKit.UIButton Button1;
+
+ public override void ViewDidLoad() {
+ base.ViewDidLoad();
+
+ View.BackgroundColor = UIKit.UIColor.White;
+
+ TextField1 = new UIKit.UITextField(new CoreGraphics.CGRect(20, 120, 200, 20))
+ {
+ Placeholder = "TextField1",
+ BorderStyle = UIKit.UITextBorderStyle.RoundedRect,
+ AccessibilityIdentifier = "TextField1"
+ };
+
+ Button1 = new UIKit.UIButton(new CoreGraphics.CGRect(20, 320, 200, 40));
+ Button1.SetTitle("Dismiss", UIKit.UIControlState.Normal);
+ Button1.BackgroundColor = UIKit.UIColor.SystemBlue;
+ Button1.AccessibilityIdentifier = "Button1";
+ Button1.TouchUpInside += (sender, e) => {
+ DismissViewController(true, null);
+ };
+
+ View.AddSubview(TextField1);
+ View.AddSubview(Button1);
+ }
+ }
+#endif
+}
diff --git a/src/Controls/tests/UITests/Tests/Issues/Issue21726.cs b/src/Controls/tests/UITests/Tests/Issues/Issue21726.cs
new file mode 100644
index 000000000000..86faa1e1ac83
--- /dev/null
+++ b/src/Controls/tests/UITests/Tests/Issues/Issue21726.cs
@@ -0,0 +1,28 @@
+using NUnit.Framework;
+using UITest.Appium;
+using UITest.Core;
+
+namespace Microsoft.Maui.AppiumTests.Issues
+{
+ public class Issue21726 : _IssuesUITest
+ {
+ public Issue21726(TestDevice device) : base(device)
+ {
+ }
+
+ public override string Issue => "Modal with a bottom sheet should not crash iOS Keyboard Scroll";
+
+ [Test]
+ public void PushViewControllerWithNullWindow()
+ {
+ this.IgnoreIfPlatforms([TestDevice.Android, TestDevice.Mac, TestDevice.Windows]);
+
+ App.WaitForElement("AddVC");
+ App.Click("AddVC");
+ App.WaitForElement("TextField1").Click();
+ App.WaitForElement("Button1").Click();
+ var mainPageElement = App.WaitForElement("AddVC");
+ Assert.NotNull(mainPageElement);
+ }
+ }
+}
diff --git a/src/Core/src/Platform/iOS/KeyboardAutoManagerScroll.cs b/src/Core/src/Platform/iOS/KeyboardAutoManagerScroll.cs
index eb91ef75040f..729bd2cf66c8 100644
--- a/src/Core/src/Platform/iOS/KeyboardAutoManagerScroll.cs
+++ b/src/Core/src/Platform/iOS/KeyboardAutoManagerScroll.cs
@@ -299,7 +299,8 @@ internal static async Task AdjustPositionDebounce()
internal static void AdjustPosition()
{
if (ContainerView is null
- || (View is not UITextField && View is not UITextView))
+ || (View is not UITextField && View is not UITextView)
+ || !View.IsDescendantOfView(ContainerView))
{
IsKeyboardAutoScrollHandling = false;
return;
@@ -311,6 +312,12 @@ internal static void AdjustPosition()
var rootViewOrigin = new CGPoint(ContainerView.Frame.GetMinX(), ContainerView.Frame.GetMinY());
var window = ContainerView.Window;
+ if (window is null)
+ {
+ IsKeyboardAutoScrollHandling = false;
+ return;
+ }
+
var intersectRect = CGRect.Intersect(KeyboardFrame, window.Frame);
var kbSize = intersectRect == CGRect.Empty ? new CGSize(KeyboardFrame.Width, 0) : intersectRect.Size;