Resize UIView/UITableView when Keyboard displays
I was having a hell of a time trying to get my UITableView to resize itself after the iPhone keyboard displayed itself. After being just a little surprised that the iPhone doesn’t resize the underlying UIView for free I figured it was up to me to do resize.
Firstly add a few variables and method declares into your ViewController.h header file:
Boolean keyboardIsShowing; CGRect keyboardBounds; - (void)resizeViewControllerToFitScreen; |
Now we need to register for the UIKeyboardWillShowNotification and the UIKeyboardWillHideNotification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; |
And these notifications need somewhere to go:
#pragma mark - #pragma mark Keyboard Handling - (void)keyboardWillShow:(NSNotification *)notification { NSDictionary *userInfo = [notification userInfo]; NSValue *keyboardBoundsValue = [userInfo objectForKey:UIKeyboardBoundsUserInfoKey]; [keyboardBoundsValue getValue:&keyboardBounds]; keyboardIsShowing = YES; [self resizeViewControllerToFitScreen]; } - (void)keyboardWillHide:(NSNotification *)note { keyboardIsShowing = NO; keyboardBounds = CGRectMake(0, 0, 0, 0); [self resizeViewControllerToFitScreen]; } |
And now add the magic method to resize the view:
- (void)resizeViewControllerToFitScreen { // Needs adjustment for portrait orientation! CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; CGRect frame = self.view.frame; frame.size.height = applicationFrame.size.height; if (keyboardIsShowing) frame.size.height -= keyboardBounds.size.height; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:0.3f]; self.view.frame = frame; [UIView commitAnimations]; } |
And super importantly de-register the ViewController from those notifications.
- (void)viewWillDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } |
See how this all comes together?
1. We register for Notifications when the Keyboard shows/hides,
2. We react when the keyboard is shown/hidden,
3. The resizeViewControllerToFitScreen method handles our resize, including animating the underlying view so it looks pretty.
There’s a few caveats:
1. I’ve not tested on Landscape mode, I’m pretty sure this will fail.
2. UIKeyboardWillShowNotification can get fired every time you enter a textbox (as I’ve only one textbox it’s not a problem for me). So you might need to look at using …TextDidBeginEditing/…TextDidEndEditing or maintaining state differently so that the view isn’t jumping all over the place.

June 2nd, 2009 at 10:57 am
[...] Resize UIView/UITableView when Keyboard displays 01.06.2009 | Posted in Computer World I was having a hell of a time trying to get my UITableView to resize itself after the iPhone keyboard displayed itself. After being just a little surprised that the iPhone doesn’t resize the underlying UIView for free I figured it was up to me to do resize. Firstly add a few variables and method declares into your ViewController.h header file: ? View Code OBJC Boolean keyboardIsShowing; CGRect keyboardBounds; – ( void ) resizeViewControllerToFitScreen; Now we ne Here is the original post: Resize UIView/UITableView when Keyboard displays [...]
October 6th, 2009 at 9:38 am
Thanks for posting this. It’s the best solution I’ve seen for dealing with hidden text fields!