iOS键盘通知

Author Avatar
新风作浪 8月 21, 2015
  • 在其它设备中阅读本文章

iOS系统键盘通知

1
2
3
4
5
6
UIKeyboardWillShowNotification // 键盘将要出现
UIKeyboardDidShowNotification // 键盘已经出现
UIKeyboardWillHideNotification // 键盘将要消失
UIKeyboardDidHideNotification // 键盘已经消失
UIKeyboardWillChangeFrameNotification // 键盘frame将要修改
UIKeyboardDidChangeFrameNotification // 键盘frame已经修改

键盘通知事件里获取userInfo key

1
2
3
4
5
6
7
8
UIKeyboardFrameBeginUserInfoKey // 开始时的键盘位置
UIKeyboardFrameEndUserInfoKey // 结束时的键盘位置
UIKeyboardAnimationCurveUserInfoKey // 动画类型
UIKeyboardAnimationDurationUserInfoKey // 动画持续事件
UIKeyboardIsLocalUserInfoKey // 是否为本地用户键盘
UIKeyboardCenterBeginUserInfoKey // 开始时的键盘中心
UIKeyboardCenterEndUserInfoKey // 结束时的键盘中心
UIKeyboardBoundsUserInfoKey // 键盘边界

关于iPad浮动键盘

在iPad上键盘可以拆分和浮动,在拆分和浮动情况下 UIKeyboardWillShowNotificationUIKeyboardWillHideNotification 两个通知并会触发,需要通过监听UIKeyboardWillChangeFrameNotification或者UIKeyboardDidChangeFrameNotification获取键盘的一些变化。

1
2
3
4
5
6
7
8
9
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardDidChangeFrameNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * notification) {
CGRect keyboardEndFrame = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect screenRect = [[UIScreen mainScreen] bounds];
if (CGRectIntersectsRect(keyboardEndFrame, screenRect)) {
// 键盘显示
} else {
// 键盘隐藏
}
}];