- (void)viewDidLoad {
[super 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 - 键盘即将弹出事件处理
- (void)keyboardWillShow:(NSNotification *)notification
{
//获取键盘信息
NSDictionary *keyBoardInfo = [notification userInfo];
//获取动画时间
CGFloat duration = [[keyBoardInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
//获取键盘的frame信息
NSValue *value = [keyBoardInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
[UIView animateWithDuration:duration animations:^{
CGRect frame = self.toolbarView.frame;
frame.origin.y = [UIScreen mainScreen].bounds.size.height - keyboardSize.height - frame.size.height;
self.toolbarView.frame = frame;
} completion:nil];
}
#pragma mark - 键盘即将隐藏事件
- (void)keyboardWillHide:(NSNotification *)notification
{
//获取键盘信息
NSDictionary *keyBoardInfo = [notification userInfo];
//获取动画时间
CGFloat duration = [[keyBoardInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
//获取键盘的frame信息
NSValue *value = [keyBoardInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
[UIView animateWithDuration:duration animations:^{
CGRect frame = self.toolbarView.frame;
frame.origin.y = [UIScreen mainScreen].bounds.size.height - keyboardSize.height - frame.size.height;
self.toolbarView.frame = frame;
} completion:nil];
}