MonoTouch.Dialog: Dismissing keyboard by touching anywhere in DialogViewController
Asked Answered
E

4

16

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?

Eadwina answered 30/5, 2012 at 20:53 Comment(0)
H
30

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;
Hizar answered 2/6, 2012 at 17:54 Comment(4)
That's brilliant. After reading your answer to #10832514 I realized my solution was likely to cause a problem down the road. Not only is yours better in that it eliminates that possibility, it also provides the exact functionality I was looking for. Thanks a million!Eadwina
you must also add dvc.CancelsTouchesInView = false; otherwise this will cancel all your other clicks on the page!Parsimonious
add tap.CancelsTouchesInView = false; to prevent the TapGestureRecognizer from canceling clicking on all the elements.Brianabriand
if we add a tap gesture, we cannot then close the label displayed when an auto complete is proposedIndole
P
2

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?

Pepe answered 30/5, 2012 at 22:16 Comment(2)
Oh yes, I remember reading your question, sorry. It's a shame about this issue: otherwise MT.D is a brilliant package. I'll wait for a little while on the off chance someone else might have an answer. If not, I'll accept yours.Eadwina
for me too. It does not seem to be hard to implement.Indole
E
1

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!

Eadwina answered 31/5, 2012 at 2:10 Comment(1)
The above will interfere with other TableView events, use the gesture recognizer, as I show in my answer in this question.Hizar
I
0

One workaround to use the dragging gesture instead of the tap as proposed (that do not interfere with the table view gestures) is to override MonoTouch.Dialog.DialogViewController.SizingSource (or MonoTouch.Dialog.DialogViewController.Source if you don't want uneven rows) and give it to the DialogViewController. I don't know if it is very clean or safe.

public class CustomTableViewSource : MonoTouch.Dialog.DialogViewController.SizingSource
{
 public CustomTableViewSource(MonoTouch.Dialog.DialogViewController dvc) : base(dvc)
 {

 }

 public override void DraggingStarted(UIScrollView scrollView)
 {
   base.DraggingStarted(scrollView);

   if (scrollView != null)
   {
     scrollView.EndEditing(true);
   }
 }

}

Indole answered 2/5, 2014 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.