OC textField键盘弹起事件
经常用到点击textFiled时弹起键盘,然后 textField工具条也要随之上升,自己做了个简单的例子
1. 监听键盘弹起收回事件
//监听键盘弹出事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//监听键盘隐藏事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
2. 实现对应方法
获取键盘的高度时一定要用** objectForKey:UIKeyboardFrameEndUserInfoKey**,切记
#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 = _chatBar.frame;
frame.origin.y = SCREENHEIGHT - keyboardSize.height - frame.size.height;
_chatBar.frame = frame;
} completion:nil];
}
#pragma mark - 键盘即将隐藏事件
- (void)keyboardWillHide:(NSNotification *)notification
{
//获取键盘信息
NSDictionary *keyBoardInfo = [notification userInfo];
//获取动画时间
CGFloat duration = [[keyBoardInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
//获取键盘的frame信息
[UIView animateWithDuration:duration animations:^{
CGRect frame = _chatBar.frame;
frame.origin.y = SCREENHEIGHT - _chatBar.height;
_chatBar.frame = frame;
} completion:nil];
}