Keyboard helper for Monotouch

Last year I started developing for Windows Phone 7 and was disappointed by the lack of form support, i.e. if you enter data into a field it you have to write code to move focus into the next field. Whilst developing a solution I stated iOS has much better support. So I was surprised when developing a similar application for iOS that it doesn’t have such support…unless someone can correct me on that?? In fact it’s worse because the keyboard covers the very field you want to type in. So if you want to put controls in a view and have the use move between them AND have them visible, you might find the following code helps. Please note this is very early days for the code so there are some hard coded sizes and probably a few issues, so please let me know if find anything or have a better mechanism. If anyone wants to contribute more formally I might place it on a public code site. Anyway here it is;

public static class KeyboardHelper
		public static void ScrollControlIntoView(UIScrollView scrollViewer, UIControl field)
			PointF nextPoint = new PointF(field.Frame.X, field.Frame.Top-30);
			scrollViewer.SetContentOffset(nextPoint, true);
		public static void InitialiseFormFields(UIScrollView scrollViewer, List<UIControl> formFields)
			if (formFields.Count>1)
				for(int index=0; index< formFields.Count-1; index++)
					UIControl field = formFields[index];
					UITextField textField = field as UITextField;
					UIControl nextField = formFields[index+1];
					field.EditingDidBegin += delegate(object sender, EventArgs e) {
					if (textField!=null)
						textField.ShouldReturn = delegate
							if (nextField is IUITextInputTraits)
							KeyboardHelper.ScrollControlIntoView(scrollViewer, nextField);
							return true;
			UIControl lastField = formFields[formFields.Count-1];
			UITextField lastTextField = lastField as UITextField;
			lastField.EditingDidBegin += delegate(object sender, EventArgs e) {
					ScrollControlIntoView(scrollViewer, lastField);
			if (lastTextField!=null)
				lastTextField.ShouldReturn = delegate
					KeyboardHelper.ScrollControlIntoView(scrollViewer, formFields[0]);
					return true;

To use the helper place your controls in a ScrollView and pass the helper an ordered list of the controls you want it to manage;

formFields = new List<UIControl>() { textBoxName, segmentedGender};
KeyboardHelper.InitialiseFormFields(scrollView, formFields);

5 thoughts on “Keyboard helper for Monotouch

  1. Klaus June 3, 2012 / 8:08 pm

    I needed the hint for SetContentOffest – thanks!

  2. gilberto January 3, 2013 / 4:58 pm

    Hi, thank you for your help, but didn`t work for me.
    I called the static method inside the helper but didn`t evoke. I put the code inside a new cs file (same namespace) and added a ref by “using”…some tip to fix it?

    • pauliom January 3, 2013 / 10:19 pm

      Could you provide some more info, sample, etc?

      • gilberto January 3, 2013 / 10:45 pm

        Hi pauliom, thank you for your reply.
        I fixed it, was my fault I forgot to create a frame to scrollview…thank you again!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s