Skip to content
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

Allow iOS Keyboard Scrolling to be turned off in Lifecycle Events #18266

Merged
merged 2 commits into from
Oct 24, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ static void OnConfigureLifeCycle(IiOSLifecycleBuilder iOS)
.OnPlatformWindowCreated((window) =>
{
window.GetWindow()?.Created();
KeyboardAutoManagerScroll.Connect();
if (!KeyboardAutoManagerScroll.ShouldDisconnectLifecycle)
KeyboardAutoManagerScroll.Connect();
})
.WillTerminate(app =>
{
Expand Down
33 changes: 26 additions & 7 deletions src/Core/src/Platform/iOS/KeyboardAutoManagerScroll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,22 @@ 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;
internal static bool ShouldDisconnectLifecycle;

/// <summary>
/// Enables automatic scrolling with keyboard interactions on iOS devices.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
public static void Connect()
{
if (TextFieldToken is not null)
Expand All @@ -55,8 +64,18 @@ public static void Connect()
DidHideToken = NSNotificationCenter.DefaultCenter.AddObserver(new NSString("UIKeyboardDidHideNotification"), DidHideKeyboard);
}

/// <summary>
/// Disables automatic scrolling with keyboard interactions on iOS devices.
/// </summary>
/// <remarks>
/// When this method is called, scrolling will not automatically happen when the keyboard comes up.
/// </remarks>
public static void Disconnect()
{
// if Disconnect is called prior to Connect, signal to not
// Connect during the Created Lifecycle event
ShouldDisconnectLifecycle = true;

if (WillShowToken is not null)
{
NSNotificationCenter.DefaultCenter.RemoveObserver(WillShowToken);
Expand Down