MonoTouch.Dialog: Dismissing keyboard by touching anywhere in DialogViewController

ⅰ亾dé卋堺 提交于 2019-11-27 21:20:29
miguel.de.icaza

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;
valdetero

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?

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!

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

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!