Skip to content

Instantly share code, notes, and snippets.

@sdpjswl
Created September 29, 2014 05:27
Show Gist options
  • Save sdpjswl/a5db9a4682d4689e14ff to your computer and use it in GitHub Desktop.
Save sdpjswl/a5db9a4682d4689e14ff to your computer and use it in GitHub Desktop.
Adjust ScrollView when keyboard appears
// call "registerForKeyboardNotifications" to enable
- (void)registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWasShown:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(scrollViewObject.contentInset.top, 0.0, kbSize.height, 0.0);
scrollViewObject.contentInset = contentInsets;
scrollViewObject.scrollIndicatorInsets = contentInsets;
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
UIEdgeInsets contentInsets = UIEdgeInsetsMake(scrollViewObject.contentInset.top, 0.0, 0.0, 0.0);
scrollViewObject.contentInset = contentInsets;
scrollViewObject.scrollIndicatorInsets = contentInsets;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment