Xamarin.iOS hide keyboard

image

In this post we will look at how we can hide the keyboard after using it for entering data in a text field. Now when you come from another platform feature might seem rather strange, but you will indeed have to handle hiding the keyboard from within your own code. In the ViewController’sViewDidLoad method of the view with the text field override the ShouldReturn method which will be called as soon as the Enter button is selected by the user:

EntryTextField.ShouldReturn = (textField) => {    textField.ResignFirstResponder ();    return true;};

The attached Lambda will hide the keyboard with an animation from the screen. Note that you could also use this handler to execute code e.g. perform a login or go to the next TextField when the user presses enter.

#

Remove on tapping the view

Another common approach is to dismiss the keyboard if the user taps anywhere in the view that is neither the TextField nor the keybaord. For this we add another few lines of code in the ViewDidLoad method:

var g = new UITapGestureRecognizer (() => View.EndEditing (true));g.CancelsTouchesInView = false; //for iOS5View.AddGestureRecognizer (g);

This will allow the user hide the keyboard by taping onto the view.

Conclusion

This blog post describes how you can hide the keyboard within an iOS application. Further it shows you how you can react when a user presses enter on the software keyboard.

You can find the entire code on GitHub.

Resources: http://forums.xamarin.com/discussion/5934/ios-newb-how-do-you-get-rid-of-the-keyboard

Updated: