Skip to content

Instantly share code, notes, and snippets.

@dutran90
Last active August 27, 2015 15:07
Show Gist options
  • Save dutran90/96149290d97a6560601f to your computer and use it in GitHub Desktop.
Save dutran90/96149290d97a6560601f to your computer and use it in GitHub Desktop.
Show&Hide Keyboard
//ViewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
#pragma mark - UITextField delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (void)keyboardWillShow:(NSNotification*)notification {
NSDictionary *info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGRect fr = _scrMain.frame;
fr.size.height = fr.size.height - kbSize.height;
_scrMain.frame = fr;
}
- (void)keyboardWillHide:(NSNotification*)notification {
NSDictionary *info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
// Write code to adjust views accordingly using kbSize.height
CGRect fr = _scrMain.frame;
fr.size.height = fr.size.height + kbSize.height;
_scrMain.frame = fr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment