MonoTouch.Dialog: Dismissing keyboard by touching

2019-01-14 12:37发布

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?

4条回答
迷人小祖宗
2楼-- · 2019-01-14 12:49

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!

查看更多
贪生不怕死
3楼-- · 2019-01-14 12:55

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;
查看更多
Melony?
4楼-- · 2019-01-14 13:03

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?

查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-14 13:11

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);
   }
 }

}

查看更多
登录 后发表回答