Robert Karl
Robert Karl

Reputation: 7826

Hiding the iPhone Keyboard in iOS 5

How do I find and remove the view corresponding to the built-in iPhone keyboard?

In this project, the user enters input into a UITextField that's always the first responder. In previous versions of iOS (2.1 --> 4, I believe) we added a listener for the

[[NSNotificationCenter defaultCenter]
                    addObserver: self
                       selector: @selector(keyboardWillShow:)
                           name: UIKeyboardWillShowNotification
                         object: nil
];

When the keyboard is about to show, we then remove it (and our textfield remains first responder).

- (void)keyboardWillShow:(NSNotification *)note {
    UIWindow* tempWindow;
    UIView* keyboard;
    UIView* minusView = nil;
    for(int c = 0; c < [[[UIApplication sharedApplication]
                                            windows] count]; c ++) {
        tempWindow = [[[UIApplication sharedApplication]
                                            windows] objectAtIndex:c];      
        // Loop through all views in the current window
        for(int i = 0; i < [tempWindow.subviews count]; i++) {
            keyboard = [tempWindow.subviews objectAtIndex:i];
            //the keyboard view description always starts with <UIKeyboard
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) {
                minusView = keyboard;
            }
        }
    }
    [minusView removeFromSuperview];
}

This approach no longer works, because in iOS 5 there isn't a view of type UIKeyboard that can be found this way.

However, there is a view with the following description <UITextEffectsWindow: 0x693cca0; frame = (0 0; 320 480); hidden = YES; opaque = NO; layer = <UIWindowLayer: 0x693cdb0>> which I have tried to remove with the removeFromSuperview and setIsHidden methods.

Any tips on removing the keyboard and keeping first responder status?

The left screenshot below is the loading screen, which depicts the buttons layout as intended. The right screencap shows how the built-in keyboard obscures the calculator buttons.

Screenshot of app (loading screen) Built-in keyboard obscuring my buttons

Upvotes: 1

Views: 1973

Answers (1)

rob mayoff
rob mayoff

Reputation: 386038

You should not be removing the keyboard. The keyboard window is private and, as you have discovered, likely to change between releases.

The correct way to get the effect you want is to set your text field's inputView property to the view that contains your calculator buttons. Then iOS will take care of showing that view for you, instead of showing the default keyboard.

This is documented in “Custom Views for Data Input” in the Text, Web, and Editing Programming Guide for iOS.

Upvotes: 6

Related Questions