2

I am currently using this code to hide the keyboard in a monotouch iOS application when something outside the input elements are tapped.

 var tap = new UITapGestureRecognizer ();

 tap.AddTarget (() =>{

    dvc.View.EndEditing (true);

 });

 dvc.View.AddGestureRecognizer (tap);

However, I would like to hide the keyboard when the user taps the top Navbar as well. I have seen this in other apps. How would I go about doing that?

servarevitas3
  • 483
  • 1
  • 6
  • 23

1 Answers1

1

Easiest way is to override TouchesEnded in your UIViewController.

This will give you any touch event inside the entire controller.

Then do something like this if you need ignore touches in a certain view:

public override void TouchesEnded(NSSet touches, UIEvent evt)
{
    base.TouchesEnded(touches, evt);

    if (evt.TouchesForView(viewYouWantToIgnore) == null) {
        //Dismiss your keyboard here
    }
}
jonathanpeppers
  • 26,115
  • 21
  • 99
  • 182
  • The views where I need this are DialogViewControllers, not UIViewControllers. Also, when I use it with a UIViewController, it still does not register a tap on the navbar. – servarevitas3 Jan 10 '13 at 15:01
  • You need to do this on the `UINavigationController` containing your inner view controller. Since you are using `MonoTouch.Dialog` though, I don't know if they expose a way to do that. – jonathanpeppers Jan 10 '13 at 15:04
  • Just checking, have you tried overriding this on your `DialogViewController`? – jonathanpeppers Jan 12 '13 at 14:21
  • Yeah, no go. Nothing even seems to trigger the event. – servarevitas3 Jan 14 '13 at 15:08
  • This question seems to allude to that as well: http://stackoverflow.com/questions/10824092/monotouch-dialog-dismissing-keyboard-by-touching-anywhere-in-dialogviewcontroll – servarevitas3 Jan 14 '13 at 15:11