Skip to content

Instantly share code, notes, and snippets.

@Splat
Last active December 11, 2015 05:09
Show Gist options
  • Save Splat/4550468 to your computer and use it in GitHub Desktop.
Save Splat/4550468 to your computer and use it in GitHub Desktop.
MonoTouch/iOS number pad keyboard needs a UIToolbar to include a button to resign the first responder of a UITextField because it doesn't include any built in way. This is a custom sub-classed UITextField which given a keyboard type that is a number pad will format a toolbar with a done button.
using System;
using MonoTouch.UIKit;
using System.Drawing;
public class CustomTextField : UITextField
{
public CustomTextField (UIKeyboardType keyboardType) {
// iPad doesn't really need it.
if (keyboardType == UIKeyboardType.NumberPad && DeviceHelper.IsIPhone()) {
UIToolbar numPadToolBar;
numPadToolBar = new UIToolbar(new RectangleF(0,0,320,27));
numPadToolBar.BarStyle = UIBarStyle.Black;
UIBarButtonItem doneButtonItem = new UIBarButtonItem ("Done", UIBarButtonItemStyle.Done, null);
doneButtonItem.Clicked += delegate(object sender, EventArgs e) {
this.ResignFirstResponder ();
};
// add in any other buttons you might have created such as "Revert"
UIBarButtonItem[] numPadToolbarButtons = new UIBarButtonItem[] {doneButtonItem};
numPadToolBar.SetItems (numPadToolbarButtons, false);
// wire it up
this.InputAccessoryView = numPadToolBar;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment