- (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)unregisterForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[self registerForKeyboardNotifications];
_viewFrame = _inputDiaryView.frame;
}
- (void)viewDidDisappear:(BOOL)animated {
[self unregisterForKeyboardNotifications];
}
- (void)keyboardWasShown:(NSNotification *)aNotification {
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView beginAnimations:TEXTVIEW_KEYBOARD context:nil];
[UIView setAnimationDuration:animationDuration];
_inputDiaryView.frame = CGRectMake(0, _viewFrame.origin.y - keyboardRect.size.height, _viewFrame.size.width, _viewFrame.size.height);
[UIView commitAnimations];
}
- (void)keyboardWillBeHidden:(NSNotification *)aNotification{
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView beginAnimations:TEXTVIEW_KEYBOARD context:nil];
[UIView setAnimationDuration:animationDuration];
_inputDiaryView.frame = _viewFrame;
[UIView commitAnimations];
}
_viewFrame:是UITextView的父控件的frame
_inputDiaryView就是UITextView的父控件