第一步:利用通知监听键盘的弹出和隐藏通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
第二步:在相应的方法中调整toolbar的frame
/**
* 监听键盘弹出
*/
- (void)keyboardWillShow:(NSNotification *)note
{
//1.取出键盘的frame
CGRect keyboardF = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//2.取出键盘弹出的时间
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//3.执行动画
[UIView animateWithDuration:duration animations:^{
self.toolbar.transform = CGAffineTransformMakeTranslation(0, -keyboardF.size.height);
}];
}
/**
* 监听键盘隐藏
*/
- (void)keyboardWillHide:(NSNotification *)note
{
//1.取出键盘的frame
CGRect keyboardF = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//2.取出键盘隐藏的时间
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//3.执行动画
[UIView animateWithDuration:duration animations:^{
self.toolbar.transform = CGAffineTransformIdentity;
}];
}
第三步:dealloc的时候去除通知
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}