views:

1050

answers:

2

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values varies. The problem is that the number pad does not contain a key for entering a decimal point.

When I lock my phone, the number pad that comes up to enter a passcode has a custom button to make an emergency call (as seen in the following screenshot):

Numberpad with custom button

Does anyone know how to create a number pad with a decimal point button or a custom button (like the emergency call button above)?

Thanks.

+1  A: 

Create a UIViewController that contains a UIView with a bunch of buttons. Now your keyboard can have whatever you want on it.

Frank Krueger
+7  A: 

There's no Apple-approved way to edit the existing keyboard. If you want them to allow it, file a feature request.

That said, it just so happens that in most applications the keyboard (instance of UIKeyboard) is a separate UIWindow, and you can iterate over the windows in the application and start adding custom subviews that respond to the appropriate touch actions. Find it by iterating over [[UIApplication sharedApplication] windows] and checking to see if the description contains the string UIKeyboard. For more info on this method and some sample code, see this answer.

Another approach is to create your own custom view and build a keyboard from scratch. Be careful if you do this, though, as it requires a lot of manual work, not only in creating the keyboard and getting the touch behavior to match Apple's, but also in any control you add that would bring up the regular keyboard - you'll need to redirect things like becomeFirstResponder to show your own keyboard, rather than Apple's.

Edit: As ZaBlanc pointed out, newer versions of iOS have a way to do this with the inputView and inputAccessoryView properties. See the UIResponder class reference for details.

Tim
UITextField supports the inputView property in 3.2+ so this is much easier to provide your own view. :-) Just replaces where the keyboard would be. inputAccessoryView lets you easily add a toolbar, too.
ZaBlanc