您的位置:首页 > 其它

OC textField键盘弹起事件

2016-06-07 18:06 417 查看

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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  textField 键盘弹起