Skip to content

Instantly share code, notes, and snippets.

@cemerson
Last active December 23, 2015 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cemerson/6642026 to your computer and use it in GitHub Desktop.
Save cemerson/6642026 to your computer and use it in GitHub Desktop.
Hack/Fix for (temporary?) Bug with IOS7 and Cordova's KeyboardShrinksView flag not working right.
// ----------- CDVViewController.m -----------------
- (void)keyboardWillShowOrHide:(NSNotification*)notif {
// Ignore this conditional statement and just assume KeyboardShrinksView=true
// if (![@"true" isEqualToString :[self settingForKey:@"KeyboardShrinksView"]]) {
// return;
// }
BOOL showEvent = [notif.name isEqualToString:UIKeyboardWillShowNotification];
CGRect keyboardFrame = [notif.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardFrame = [self.view convertRect:keyboardFrame fromView:nil];
CGRect newFrame = self.view.bounds;
if (showEvent) {
// Add, don't subtract, the keyboard height to webview frame
// newFrame.size.height -= keyboardFrame.size.height;
newFrame.size.height += keyboardFrame.size.height;
// don't allow frame height to exceed 1024 (iPad)
if(newFrame.size.height > 1024) newFrame.size.height = 1024;
self.webView.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); //-keyboardFrame.size.height, 0);
[webView stringByEvaluatingJavaScriptFromString:@"toggleKeyboardVisibleClass()"];
} else {
self.webView.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
[webView stringByEvaluatingJavaScriptFromString:@"toggleKeyboardVisibleClass('OFF')"];
}
// NSLog(@"keyboardWillShowOrHide() [webView height:%f | keyboardFrame height:%f]",newFrame.size.height, keyboardFrame.size.height);
// this seems to be needed every pass otherwise the device (iPad)
// starts shrinking the webview again
self.webView.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
self.webView.frame = newFrame;
}
// ----------- index.js (or whatever.js) -----------------
/* ----------------------------------------------------------- /
toggleKeyboardVisibleClass
Adds "keyboard_visible" class to <body> as IOS shows/hides the keyboard
Note: assumes jQuery is loaded
/ ----------------------------------------------------------- */
function toggleKeyboardVisibleClass(onOrOff){
try{
switch(onOrOff){
case 'OFF':
$('body').removeClass('keyboard_visible');
break;
default:
$('body').addClass('keyboard_visible');
break;
}
}catch(e){
// your error handling of choice ...
}
}
@mrsubtle
Copy link

Hey Chris - you know if this works in PhoneGap 3.3.0? I can't find keyboardWillShowOrHide in the 3.3.0 CDVViewController.m file - is this a new Method to add?

@brain56
Copy link

brain56 commented Apr 25, 2014

Hey @mrsubtle - did you get an answer to your question? I'm also interested in finding out. I'm using this for PhoneGap 3.4.0. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment