From 3393eb444f8b1f2ce850574a0b562dfb15a465dd Mon Sep 17 00:00:00 2001 From: tj-devel709 Date: Mon, 23 Oct 2023 10:51:19 -0500 Subject: [PATCH 1/2] Add spacing --- .../Platform/iOS/KeyboardAutoManagerScroll.cs | 44 ++++++++++++++++--- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/src/Core/src/Platform/iOS/KeyboardAutoManagerScroll.cs b/src/Core/src/Platform/iOS/KeyboardAutoManagerScroll.cs index 69d484258830..928bebc19b56 100644 --- a/src/Core/src/Platform/iOS/KeyboardAutoManagerScroll.cs +++ b/src/Core/src/Platform/iOS/KeyboardAutoManagerScroll.cs @@ -32,15 +32,34 @@ public static class KeyboardAutoManagerScroll static CGRect? CursorRect = null; internal static bool IsKeyboardShowing = false; static int TextViewTopDistance = 20; - static int DebounceCount = 0; - static NSObject? WillShowToken = null; - static NSObject? WillHideToken = null; - static NSObject? DidHideToken = null; - static NSObject? TextFieldToken = null; - static NSObject? TextViewToken = null; - + static int DebounceCount; + static NSObject? WillShowToken; + static NSObject? WillHideToken; + static NSObject? DidHideToken; + static NSObject? TextFieldToken; + static NSObject? TextViewToken; + static bool? ShouldConnect; + + /// + /// Enables automatic scrolling with keyboard interactions on iOS devices. + /// + /// + /// This method is being called by default on iOS and will scroll the page when the keyboard + /// comes up. Call the method 'KeyboardAutoManagerScroll.Disconnect()' + /// to remove this scrolling behavior. + /// public static void Connect() { + // if Disconnect was called prior to the first Connect + // call in the Created Lifecycle event, do not connect + if (ShouldConnect is false) + { + ShouldConnect = true; + return; + } + + ShouldConnect = true; + if (TextFieldToken is not null) return; @@ -55,8 +74,19 @@ public static void Connect() DidHideToken = NSNotificationCenter.DefaultCenter.AddObserver(new NSString("UIKeyboardDidHideNotification"), DidHideKeyboard); } + /// + /// Disables automatic scrolling with keyboard interactions on iOS devices. + /// + /// + /// When this method is called, scrolling will not automatically happen when the keyboard comes up. + /// public static void Disconnect() { + // if Disconnect is called prior to Connect, signal to not + // Connect during the Created Lifecycle event + if (ShouldConnect is null) + ShouldConnect = false; + if (WillShowToken is not null) { NSNotificationCenter.DefaultCenter.RemoveObserver(WillShowToken); From 2556e7e0ed392e6dd6b62a00587989ebadd78c17 Mon Sep 17 00:00:00 2001 From: tj-devel709 Date: Mon, 23 Oct 2023 16:21:22 -0500 Subject: [PATCH 2/2] Use a better implementation --- .../AppHostBuilderExtensions.iOS.cs | 3 ++- .../src/Platform/iOS/KeyboardAutoManagerScroll.cs | 15 ++------------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/src/Core/src/Hosting/LifecycleEvents/AppHostBuilderExtensions.iOS.cs b/src/Core/src/Hosting/LifecycleEvents/AppHostBuilderExtensions.iOS.cs index 2837e13b0a44..a7044fcb97a5 100644 --- a/src/Core/src/Hosting/LifecycleEvents/AppHostBuilderExtensions.iOS.cs +++ b/src/Core/src/Hosting/LifecycleEvents/AppHostBuilderExtensions.iOS.cs @@ -19,7 +19,8 @@ static void OnConfigureLifeCycle(IiOSLifecycleBuilder iOS) .OnPlatformWindowCreated((window) => { window.GetWindow()?.Created(); - KeyboardAutoManagerScroll.Connect(); + if (!KeyboardAutoManagerScroll.ShouldDisconnectLifecycle) + KeyboardAutoManagerScroll.Connect(); }) .WillTerminate(app => { diff --git a/src/Core/src/Platform/iOS/KeyboardAutoManagerScroll.cs b/src/Core/src/Platform/iOS/KeyboardAutoManagerScroll.cs index 928bebc19b56..66c26edd9ddb 100644 --- a/src/Core/src/Platform/iOS/KeyboardAutoManagerScroll.cs +++ b/src/Core/src/Platform/iOS/KeyboardAutoManagerScroll.cs @@ -38,7 +38,7 @@ public static class KeyboardAutoManagerScroll static NSObject? DidHideToken; static NSObject? TextFieldToken; static NSObject? TextViewToken; - static bool? ShouldConnect; + internal static bool ShouldDisconnectLifecycle; /// /// Enables automatic scrolling with keyboard interactions on iOS devices. @@ -50,16 +50,6 @@ public static class KeyboardAutoManagerScroll /// public static void Connect() { - // if Disconnect was called prior to the first Connect - // call in the Created Lifecycle event, do not connect - if (ShouldConnect is false) - { - ShouldConnect = true; - return; - } - - ShouldConnect = true; - if (TextFieldToken is not null) return; @@ -84,8 +74,7 @@ public static void Disconnect() { // if Disconnect is called prior to Connect, signal to not // Connect during the Created Lifecycle event - if (ShouldConnect is null) - ShouldConnect = false; + ShouldDisconnectLifecycle = true; if (WillShowToken is not null) {