Unsolicited Feedback

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*!
 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:.

1
2
3
4
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(modifyKeyboard:) 
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

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.


10 Comments

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.

Posted by fernyb on 11 March 2009 @ 3pm

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.

Posted by Ivan on 16 September 2009 @ 11am

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.

Posted by Tyler on 22 November 2009 @ 5am

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!

vehicle

Posted by zesisefrawite on 8 January 2011 @ 7am

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 :)

Posted by Nick on 27 January 2012 @ 7pm

It’s an amazing paragraph in support of all the internet
people; they will get advantage from it I am sure.

Posted by google api code on 26 April 2014 @ 4am

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

Posted by Dixie on 16 May 2014 @ 7am

I am regular visitor, how are you everybody?
This paragraph posted at this website is truly fastidious.

Here is my blog post anime series must watch list

Posted by anime series must watch list on 19 July 2014 @ 1am

Porn doggy style anal sex. Twin sister lesbian sex

Feel free to surf to my web page – fuck my sister coupons

Posted by fuck my sister coupons on 26 July 2014 @ 2am

This info is invaluable. When can I find out more?

my webpaage :: Free MLTD Coupon code

Posted by Free MLTD Coupon code on 29 July 2014 @ 2am

Leave a Comment