- (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{
}
注意 添加了观察者就必须有对应的触发方法,虽然为空,不能不写
ios键盘高度监听
最新推荐文章于 2017-03-01 10:02:41 发布