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

[iOS] Fix SearchBar focus issues #11698

Merged
merged 12 commits into from
Mar 20, 2023
16 changes: 16 additions & 0 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ protected override void ConnectHandler(MauiSearchBar platformView)
platformView.TextSetOrChanged += OnTextPropertySet;
platformView.OnMovedToWindow += OnMovedToWindow;
platformView.ShouldChangeTextInRange += ShouldChangeText;
platformView.OnEditingStarted += OnEditingStarted;
platformView.EditingChanged += OnEditingChanged;
platformView.OnEditingStopped += OnEditingStopped;

base.ConnectHandler(platformView);
}
Expand All @@ -47,7 +49,9 @@ protected override void DisconnectHandler(MauiSearchBar platformView)
platformView.TextSetOrChanged -= OnTextPropertySet;
platformView.ShouldChangeTextInRange -= ShouldChangeText;
platformView.OnMovedToWindow -= OnMovedToWindow;
platformView.OnEditingStarted -= OnEditingStarted;
platformView.EditingChanged -= OnEditingChanged;
platformView.OnEditingStopped -= OnEditingStopped;

base.DisconnectHandler(platformView);
}
Expand Down Expand Up @@ -177,6 +181,12 @@ bool ShouldChangeText(UISearchBar searchBar, NSRange range, string text)
return newLength <= VirtualView?.MaxLength;
}

void OnEditingStarted(object? sender, EventArgs e)
{
if (VirtualView != null)
jsuarezruiz marked this conversation as resolved.
Show resolved Hide resolved
VirtualView.IsFocused = true;
}

void OnEditingChanged(object? sender, EventArgs e)
{
if (VirtualView == null || _editor == null)
Expand All @@ -187,6 +197,12 @@ void OnEditingChanged(object? sender, EventArgs e)
UpdateCancelButtonVisibility();
}

void OnEditingStopped(object? sender, EventArgs e)
{
if (VirtualView != null)
jsuarezruiz marked this conversation as resolved.
Show resolved Hide resolved
VirtualView.IsFocused = false;
}

void UpdateCancelButtonVisibility()
{
if (PlatformView.ShowsCancelButton != VirtualView.ShouldShowCancelButton())
Expand Down