Skip to content

Instantly share code, notes, and snippets.

@JigsChanchiya
Created October 15, 2013 12:08
Show Gist options
  • Save JigsChanchiya/6990562 to your computer and use it in GitHub Desktop.
Save JigsChanchiya/6990562 to your computer and use it in GitHub Desktop.
Keybord notifications in OS
/*
UIKeyboardWillShowNotification
UIKeyboardDidShowNotification
UIKeyboardWillHideNotification
UIKeyboardDidHideNotification
*/
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:@"UIKeyboardWillShowNotification"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:@"UIKeyboardDidHideNotification"
object:nil];
}
- (void) keyboardWillShow:(NSNotification *)note {
NSDictionary *userInfo = [note userInfo];
CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
NSLog(@"Keyboard Height: %f Width: %f", kbSize.height, kbSize.width);
// move the view up by 30 pts
CGRect frame = self.view.frame;
frame.origin.y = -30;
[UIView animateWithDuration:0.3 animations:^{
self.view.frame = frame;
}];
}
- (void) keyboardDidHide:(NSNotification *)note {
// move the view back to the origin
CGRect frame = self.view.frame;
frame.origin.y = 0;
[UIView animateWithDuration:0.3 animations:^{
self.view.frame = frame;
}];
}
- (void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
// uncomment for non-ARC:
// [super dealloc];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment