您的位置:首页 > 移动开发 > IOS开发

ios键盘高度监听

2016-03-08 16:06 447 查看
- (void)keyboardNotifiation{
//注册键盘出现的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
//键盘变化
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillChange:)
name:UIKeyboardWillChangeFrameNotification object:nil];
//键盘退出
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification *)notification{

}

- (void)keyboardWillChange:(NSNotification *)notification{
NSDictionary *info = [notification userInfo];
CGRect beginKeyboardRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect endKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat yOffset = endKeyboardRect.origin.y - beginKeyboardRect.origin.y;
CGPoint offset = self.tableView.contentOffset;
if (yOffset < 0) {
offset.y -= yOffset;
if (offset.y < 0) {
offset.y = 0;
}
}
[self.tableView setContentOffset:offset animated:YES];
}

- (void)keyboardWillBeHidden:(NSNotification *)notification{

}
注意 添加了观察者就必须有对应的触发方法,虽然为空,不能不写
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: