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)
		{
			System.Diagnostics.Debug.Assert(scrollViewer!=null);
			System.Diagnostics.Debug.Assert(formFields!=null);
			
			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) {
						ScrollControlIntoView(scrollViewer,field);
					};
					
					if (textField!=null)
					{
						textField.ShouldReturn = delegate
						{
							
							field.ResignFirstResponder();
								
							if (nextField is IUITextInputTraits)
									nextField.BecomeFirstResponder();
									
							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
				{
					lastField.ResignFirstResponder();
					
					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);
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

5 Responses to Keyboard helper for Monotouch

  1. Thank you… just… thank you…

  2. Klaus says:

    I needed the hint for SetContentOffest – thanks!

  3. gilberto says:

    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?

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s