-
-
Save xxd/2680030 to your computer and use it in GitHub Desktop.
Locating the keyboard (UIKeyboard) on iOS 4 and iOS 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -------------for iOS 5--------------- | |
| - (void)viewDidLoad | |
| { | |
| [super viewDidLoad]; | |
| [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidChangeFrameNotification object:nil]; | |
| } | |
| //dont forget to [[NSNotificationCenter defaultCenter] removeObserver:self]; on viewDidUnload | |
| - (void)viewDidUnload | |
| { | |
| [super viewDidUnload]; | |
| // Release any retained subviews of the main view. | |
| [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| } | |
| -------------for iOS 4--------------- | |
| - (void)someSetupMethod { | |
| [[NSNotificationCenter defaultCenter] addObserver:self | |
| selector:@selector(keyboardWillShow:) | |
| name:UIKeyboardWillShowNotification object:nil]; | |
| } | |
| - (void)someTeardownMethod { | |
| [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; | |
| } | |
| - (void)keyboardWillShow { | |
| // We must perform on delay (schedules on next run loop pass) for the keyboard subviews to be present. | |
| [self performSelector:@selector(addHideKeyboardButtonToKeyboard) withObject:nil afterDelay:0]; | |
| } | |
| - (void)addHideKeyboardButtonToKeyboard { | |
| // Locate non-UIWindow. | |
| UIWindow *keyboardWindow = nil; | |
| for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) { | |
| if (![[testWindow class] isEqual:[UIWindow class]]) { | |
| keyboardWindow = testWindow; | |
| break; | |
| } | |
| } | |
| if (!keyboardWindow) return; | |
| // Locate UIKeyboard. | |
| UIView *foundKeyboard = nil; | |
| for (UIView *possibleKeyboard in [keyboardWindow subviews]) { | |
| // iOS 4 sticks the UIKeyboard inside a UIPeripheralHostView. | |
| if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) { | |
| possibleKeyboard = [[possibleKeyboard subviews] objectAtIndex:0]; | |
| } | |
| if ([[possibleKeyboard description] hasPrefix:@"<UIKeyboard"]) { | |
| foundKeyboard = possibleKeyboard; | |
| break; | |
| } | |
| } | |
| if (foundKeyboard) { | |
| // Add the button to foundKeyboard. | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment