- (void)keyboardWillShown:(NSNotification *)aNotification {
// 获得键盘大小
NSDictionary *info = [aNotification userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
[UIView beginAnimations:nil context:NULL];
// 设置动画
[UIView setAnimationDuration:0.3];
// 将toolBar的位置放到键盘上方
CGRect frame = toolBar.frame;
frame.origin.y -= keyboardSize.height;
toolBar.frame = frame;
//调整textView的高度
frame = textView.frame;
frame.size.height -= keyboardSize.height;
textView.frame = frame;
[UIView commitAnimations];
NSRange endRange;
endRange.location = [textView.text length];
endRange.length = 1;
// 将textView滚动到最后
if(endRange.location > 0)
[textView scrollRangeToVisible:endRange];
}
- (void)keyboardWillHidden:(NSNotification *)aNotification {
NSDictionary *info = [aNotification userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect frame = toolBar.frame;
frame.origin.y += keyboardSize.height;
toolBar.frame = frame;
frame = textView.frame;
frame.size.height += keyboardSize.height;
textView.frame = frame;
[UIView commitAnimations];
NSRange endRange;
endRange.location = [textView.text length];
endRange.length = 1;
if(endRange.location > 0)
[textView scrollRangeToVisible:endRange];
}