How to Customize UIKeyboard by Adding Subviews

So, you want to get your personal touch into the apparently unreachable iPhone keyboard, something along the line of the image below maybe. Read on.

Custom UIKeyboard

You have two possible approaches.

Approach 1: Adding the custom view on top of UIKeyboard view

You can create the custom view you want, position it above the UIKeyboard and animate it the same way UIKeyboard is. This means your code will be constantly listening to the UIKeyboardWillShow and UIKeyboadWillHide notifications in order to show and hide the custom view along with the keyboard. Also you have the hassle of having to write all the animation code for the custom view to mimic the keyboard going up and down.

This approach will work in most situations but my experience tells me that in certain situations when alternating between text fields of a form, the UIKeyboardWillHide and UIKeyboardWillShow notifications are sent so quickly that the UIkeyboard doesn’t go down. Though, our custom view goes down and comes up very quickly. If you, reading this, have any idea how to prevent this behavior, leave it in the comments.

Approach 2: Adding the custom view to the UIKeyboard view itself

Apple keeps its keyboard implementation private for some reason. So, you know, you shouldn’t be modifying UIKeyboard’s inner subviews. Anyway, you shouldn’t doesn’t mean you can’t.

You start with the same custom view you had in the first approach, but you will be adding it to the UIKeyboard subviews instead. This will save you from all the hassle of animating the custom view so that it has a similar behavior to the UIKeyboard’s.

The following block of code describes how to had your custom view to the UIKeyboard.

 As of iPhone SDK 2.2.1:
 Keyboard view is in a separated window from our application main window.
 We're digging through the unknown here, but if for some wacky reason this
 code is broke, the only side effect will be that the keyboard dismiss 
 button will not be shown.
- (void)modifyKeyboard:(NSNotification *)notification 
    NSValue *v = [[notification userInfo] valueForKey:UIKeyboardBoundsUserInfoKey];
    CGRect kbBounds = [v CGRectValue];
    for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
        // Now iterating over each subview of the available windows
        for (UIView *keyboard in [keyboardWindow subviews]) {
            // Check to see if the description of the view we have referenced is UIKeyboard.
            // If so then we found the keyboard view that we were looking for.
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) {
                // Your view specific code for the custom view goes here
                UIView *customView = [self viewForDismissKeyboard:kbBounds];
                // End of custom view code
                // Set a tag so that if you want to remove the view later, it is a lot easier.
                [customView setTag:666]; 
                // Add the desired subview to the keyboard.
                [keyboard addSubview:customView];
                // Unregister as observer if you don't want to change the keyboard again
                // and leave the custom view across the whole application.
                [[NSNotificationCenter defaultCenter] removeObserver:self];

Where do you add this code?

Since my application shares the same keyboard type across the whole app, I put this code in application delegate so that the code runs only the first time the keyborad is requested—note that I’m removing the observer in line 36. Be sure to add the following line of code at the end of application delegate applicationDidFinishLaunching:.

    [[NSNotificationCenter defaultCenter] addObserver:self

But if your application uses, let’s say a numeric and a text keyboard, and you want to add a decimal point just to the numeric keyboard, you will want to put this code in the view controller or view that holds the numeric text field. Then, after leaving the view, you shall make sure that you remove the view (controller) as an observer for the UIKeyboardWillShow and UIKeyboardWillHide notifications so that it does not affect other keyboard types. I suggest you to see UIViewController viewWillAppear and viewWillDisappear methods add/remove the custom view and unregister for UIKeyboardWillShow and UIKeyboardWillHide notifications.

If you need further help adapting this code to your application, be at will to contact me.

Published by jpedroso

Unsolicited Feedback is a blog by Jorge Pedroso, a software developer, conspirator, user interface and user experience designer aspirant focused on Mac OS X and iPhone platforms. Here he expresses, mostly, opinionated ramblings on software development, code, user experience and interface design.

Join the Conversation


  1. How do you add a toolbar above the keyboard? I can add a toolbar above the keyboard but it doesn’t respond and I’m about to give up already, I can’t figure it out.

  2. In Approach 1 you ask how to stop your keyboard going down and back up again quickly. I haven’t tried this but I would imagine that all you’d have to do is have a short delay (a sort of debounce) when the keyboard is told to go down. If the ‘up’ notification arrives during this short delay then you just don’t activate the down animation. The only drawback to this method is that the keyboard takes a split second longer to go away which isn’t a bad thing really.

  3. The way I usually stop my custom view from quickly disappearing and reappearing is to add a flag ivar (ie mIgnoreKeyboardEvent) to whatever controller is listening to keyboard hide and show notifications. I implement the text field and text view delegate methods to set this flag to YES whenever editing begins and also check for it in the keyboard notification methods. In whatever method is wired to my view’s keyboard dismissal button (or whatever other actions require the keyboard to be dismissed), I set the flag to NO.

  4. Pukkah, they straits to be taught that filing lawsuits is not the avenue to draw to a close piracy. As an choice, it’s to bestowal something mastery than piracy. Like placidity of use. It’s indubitably a twist of fate easier to utter iTunes than to search the Internet with jeopardy of malware and then crappy inimitability, but if people are expected to equip for all to see of the closet to be loads and convoy to with a view ages, it’s not in the chips to work. They a old crumpet warrant a cut together forwards people sire software and Cobweb sites that get to it ridiculously trouble-free to infringer, and up the quality. If that happens, then there demand be no stopping piracy. But they’re too politic and appalled of losing. Risks compel to be enchanted!


  5. Hey, thanks for this tutorial. Unfortunately I have one little problem. I tried, as you suggested, to remove the custom view in viewWillDisappear. But when present another view, the overlay (which I have added to the keyboard) is still there! What should I do? Thanks in advance đŸ™‚

  6. Hi, every time i used to check webpage posts here early in the dawn, since i enjoy
    to gain knowledge of more and more.

Leave a comment

Your email address will not be published. Required fields are marked *