Skip to content

Commit 40ec49b

Browse files
committed
Generalize HideSoftInputOnTapped on Android and iOS to support 3rd party input controls.
1 parent 3944004 commit 40ec49b

File tree

3 files changed

+10
-22
lines changed

3 files changed

+10
-22
lines changed

src/Controls/src/Core/ContentPage/HideSoftInputOnTappedChanged/HideSoftInputOnTappedChangedManager.Android.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Android.Widget;
44
using Microsoft.Maui.Graphics;
55
using AView = Android.Views.View;
6+
using AViewGroup = Android.Views.ViewGroup;
67

78
namespace Microsoft.Maui.Controls
89
{
@@ -43,21 +44,12 @@ page.Handler is IPlatformViewHandler pvh &&
4344
// This is called from InputViews as they are added to the visual tree
4445
IDisposable? SetupHideSoftInputOnTapped(AView aView)
4546
{
46-
if (aView is SearchView sv &&
47-
sv.GetFirstChildOfType<EditText>() is EditText editText)
47+
if (aView is AViewGroup vg &&
48+
vg.GetFirstChildOfType<EditText>() is {} editText)
4849
{
4950
aView = editText;
5051
}
5152

52-
if (aView is AndroidX.AppCompat.Widget.SearchView svX &&
53-
svX.GetFirstChildOfType<EditText>() is EditText editTextX)
54-
{
55-
aView = editTextX;
56-
}
57-
58-
if (aView is null)
59-
return null;
60-
6153
if (!FeatureEnabled)
6254
return null;
6355

src/Controls/src/Core/ContentPage/HideSoftInputOnTappedChanged/HideSoftInputOnTappedChangedManager.iOS.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,18 @@ namespace Microsoft.Maui.Controls
77
{
88
partial class HideSoftInputOnTappedChangedManager
99
{
10-
internal IDisposable? SetupHideSoftInputOnTapped(UIView uIView)
10+
internal IDisposable? SetupHideSoftInputOnTapped(UIView uiView)
1111
{
12-
if (!FeatureEnabled || uIView.Window is null)
12+
if (!FeatureEnabled || uiView.Window is null)
1313
return null;
1414

15-
if (uIView is UISearchBar searchBar &&
16-
searchBar.GetSearchTextField() is UIView textField)
17-
{
18-
uIView = textField;
19-
}
15+
var firstResponder = uiView.FindFirstResponder(v => v is UITextField or UITextView);
2016

21-
if (uIView is null)
17+
if (firstResponder is null)
2218
return null;
2319

2420
return ResignFirstResponderTouchGestureRecognizer
25-
.Update(uIView);
21+
.Update(firstResponder);
2622
}
2723
}
2824
}

src/Core/src/Platform/iOS/ViewExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,12 +889,12 @@ internal static void ChangeFocusedView(this UIView view, UIView? newView)
889889
return null;
890890
}
891891

892-
internal static UIView? FindFirstResponder(this UIView? superview)
892+
internal static UIView? FindFirstResponder(this UIView? superview, Func<UIView, bool>? predicate = null)
893893
{
894894
if (superview is null)
895895
return null;
896896

897-
if (superview.IsFirstResponder)
897+
if (superview.IsFirstResponder && (predicate?.Invoke(superview) ?? true))
898898
return superview;
899899

900900
foreach (var subview in superview.Subviews)

0 commit comments

Comments
 (0)