Last active
August 27, 2015 15:07
-
-
Save dutran90/96149290d97a6560601f to your computer and use it in GitHub Desktop.
Show&Hide Keyboard
This file contains 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
//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