Skip to content

Instantly share code, notes, and snippets.

@VincentH-Net
Created January 10, 2014 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save VincentH-Net/8352290 to your computer and use it in GitHub Desktop.
Save VincentH-Net/8352290 to your computer and use it in GitHub Desktop.
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.
/// <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