Missing DONE button in iOS
Tutorial for adding DONE button for numeric keypads
When you are using or developing an iOS app, do you have the following question: Where is the DONE button for numeric keypad? Let me elaborate more:
When you are inputing some numbers, you will see the keypad like this, will you wonder what to do when you finish the number? Do you expect there is a “DONE button” somewhere?
Here is what apple provides for english characters keyboard, you always find the DONE or RETURN button at the right lower corner which you can tap when you are done. Simply intuitive.
When I am doing a update to my real estate calculator app, as most of the input are number, I used the numeric keypad a lot. I want user to have a easy and intuitive way to indicate they are done with the number input. So where is the DONE button for the numeric keypad? Then I look at some popular app to see how they solve this problem, this is zillow mortgage app and Trulia mortgage app, basically they added a DONE button in a extra row, also with the prev/next button. Jobs get done, but it seems make the keypad not stylish, is there a better way?
Wait, there is a empty button space in the left lower corner, is that a perfect place to put a DONE button there?
Originally I would like to implement my own DONE button, then I just came across with this awesome gist. He did a good job to add the DONE button, thanks Matt. So I am going to provide a short tutorial for adding the DONE button.
This is the final image of the numeric keypad, which matching the style too.
I am not sure why Apple does not include the DONE button in the standard SDK, as this will create fragmentation as it is up to the developers to decide what to implement, I really hope that it will be fixed in coming iOS 8. Let me know if you know there is any reason not including the DONE button in the standard SDK.
Get Roland Chan’s stories in your inbox
Join Medium for free to get updates from this writer.
Here is a short tutorial of using this gist. First, assume you already get a xcode project and some basic knowledge of iOS programming, then you go to visit gist and get two files: NumberPadDoneBtn.h and NumberPadDoneBtn.m, then copy these two files into your project area.
First, import the header file in the viewController.m file.
In the storyboard, drag a textfield to your desired view controller, in the attribute inspector on the right panel, select “number pad” for the keyboard.
If you compile and run it now, then when you click on the textField, then you will see this:
This is the default iOS behavior, now, we want to get a handle of the textField, you can open the assistant view, the control-drag the textField into the viewController file.
In the viewDidLoaded method:
First, create the numberPadDoneBtn using the initWithFrame method, the frame you provided does not matter as the code will calculate the correct frame for the button programmatically. Then you need to set the textField’s inputAccessoryView to numberPadDoneBtn. The inputAccessoryView is a way Apple provide for adding accessory view in any input method, which in this case, it is adding a DONE button accessory to number keypad. Details can be found in here.
Then it is done! Is that too simple? The reason it is so simple as the gist code will take care of all the heavy logic like finding the firstResponder and resign it. This will make the code really clean.
Thanks for reading and hope you enjoy it.
Lastly, I like iOS programming and always want to learn more about iOS programming, one of the best way to learn is to teach and share knowledge. So I would like to start a iOS tutorial web site, and my idea is to use a series of tutorials (like 15-20 tutorials) to finish an fully-functional semi complicated app. All the tutorials will be part of the app, and each tutorial will focus on 1-2 specific iOS topic. So at the end of all the tutorials, you will finish a fully-functional iOS app. I am still at the phase of planning it, so if you have any comments like what kind of app you like or any ideas, please tweet me @chan0123. Any comments are welcome.









