添加监听(最好放在ViewWillAppear里, 在视图将要消失时移除监听)
这里监听键盘Frame的变化而不是监听键盘的显示和隐藏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
实现方法
_toolBar是要移动的View控件
#pragma mark - 键盘Frame改变时调用
-(void)keyboardFrameChange:(NSNotification *)note
{
CGFloat durtion = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGRect frame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
if (frame.origin.y == self.view.frame.size.height)
{
// 没有弹出键盘
[UIView animateWithDuration:durtion animations:^{
_toolBar.transform = CGAffineTransformIdentity;
}];
}
else
{
// 弹出键盘
// 工具条向上移动键盘的高度
[UIView animateWithDuration:durtion animations:^{
_toolBar.transform = CGAffineTransformMakeTranslation(0, -frame.size.height);
}];
}
}