Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MonoTouch.Dialog: Dismissing keyboard by touching anywhere in DialogViewController

NOTE: There are two similar SO questions (1) (2), but neither of them provides an answer.

TL;DR: How can one dismiss the keyboard in a MonoTouch.Dialog by letting the user touch any empty space in the view?

I'm writing an app using MonoTouch.Dialog and a UITabBarController. One of my tabs is "Settings"...

Settings screen

When the user starts typing, the keyboard obstructs the tabbar...

enter image description here

Using MonoTouch.Dialog, the only way to dismiss the keyboard is to go to the last field and press the "return" key. Considering the fact that the user cannot press any tab until the keyboard is gone, I would like a better way to do it. Namely, to dismiss if the user taps anywhere else on the screen.

Without MonoTouch.Dialog, it's a snap: simply override TouchesBegan and call EndEditing. But this doesn't work with MT.D. I've tried subclassing DialogViewController and overriding TouchesBegan there, but it doesn't work. I'm currently at a loss.

Or, I wonder, would I be better off ditching the tabbar so I can use a UINavigationController with a "Back" button on top, which won't be hidden by the keyboard?

like image 247
Mike Lorenz Avatar asked May 30 '12 20:05

Mike Lorenz


3 Answers

I suggest you use a tap gesture recognizer that will not cause interference with the TableView event handlers:

var tap = new UITapGestureRecognizer ();
tap.AddTarget (() => dvc.View.EndEditing (true));
dvc.View.AddGestureRecognizer (tap);
tap.CancelsTouchesInView = false;
like image 67
miguel.de.icaza Avatar answered Oct 23 '22 19:10

miguel.de.icaza


You missed my question about it also: Can the keyboard be dismissed by touching outside of the cell in MonoTouch.Dialog? :-)

This is my #1 feature request for MonoTouch.Dialog.

To answer your question: No. It is not possible. I have searched and asked around and have not found any answers.

I assume because it is just a sectioned (grouped) table and if it wasn't sectioned, there wouldn't be any spot to click. However, that is just my speculation.

I wish that miguel or someone that works on monotouch would answer this and say if it is even possible. Possibly a future enhancement?

like image 31
valdetero Avatar answered Oct 23 '22 18:10

valdetero


I figured out a workaround that satisfies me well enough, so I'm answering my own question.

// I already had this code to set up the dialog view controller.
var bc = new BindingContext (this, settings, "Settings");
var dvc = new DialogViewController (bc.Root, false);

// **** ADD THIS ****
dvc.TableView.DraggingStarted += (sender, e) => {
    dvc.View.EndEditing (true);
};

This will dismiss the keyboard whenever the user drags the view a little bit. There's no touch event I could find associated with the tableview, so this is the next best thing. I'd welcome any other ideas. Cheers!

like image 1
Mike Lorenz Avatar answered Oct 23 '22 18:10

Mike Lorenz