KeyboardDismissGestureRecognizer automatically dismisses the onscreen keyboard in Xamarin.iOS when a user taps outside an editable view.
Simply add a KeyboardDismissGestureRecognizer to your application's main window in FinishedLaunching.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// To automatically dismiss the onscreen keyboard in iOS when a user taps outside an editable view, | |
/// add a KeyboardDismissGestureRecognizer to your application's main window in FinishedLaunching, e.g.: | |
/// Window.AddGestureRecognizer(new KeyboardDismissGestureRecognizer()); | |
/// </summary> | |
public class KeyboardDismissGestureRecognizer : UITapGestureRecognizer | |
{ | |
public KeyboardDismissGestureRecognizer() : base(() => { }) { CancelsTouchesInView = false; } | |
public override void TouchesBegan(NSSet touches, UIEvent evt) | |
{ | |
base.TouchesBegan(touches, evt); | |
var touch = evt.AllTouches.AnyObject as UITouch; | |
if (touch != null && touch.View != null && !(touch.View.CanBecomeFirstResponder) && View != null) View.EndEditing(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment