Swift4 分割キーボード判定
分割キーボードが出たり入ったりした時の判定を調べていて、Notificationで飛ぶ通知次第で処理を簡単に分けれるでしょと思ってたらそうでもなかったのでメモ。
分割キーボードが出たり消えたりした時の通知はUIKeyboardWillChangeFrameNotification
ないしは UIKeyboardDidChangeFrameNotification
に飛びます。しかし通常のキーボードが出た時も同じ通知が飛んでしまうので、通知を飛ばした先でキーボードの状況によって処理を分けなければいけないみたいですね。
その中で、userInfo
で取得できる値には以下のようなものがありました。値はとりあえず適当
- 分割キーボード出現時
[AnyHashable("UIKeyboardCenterBeginUserInfoKey"): NSPoint: {384, 1159.5}, AnyHashable("UIKeyboardIsLocalUserInfoKey"): 1, AnyHashable("UIKeyboardCenterEndUserInfoKey"): NSPoint: {384, 588.5}, AnyHashable("UIKeyboardBoundsUserInfoKey"): NSRect: {{0, 0}, {768, 271}}, AnyHashable("UIKeyboardFrameEndUserInfoKey"): NSRect: {{0, 453}, {768, 271}}, AnyHashable("UIKeyboardAnimationCurveUserInfoKey"): 7, AnyHashable("UIKeyboardFrameBeginUserInfoKey"): NSRect: {{0, 1024}, {768, 271}}, AnyHashable("UIKeyboardAnimationDurationUserInfoKey"): 0.25]
- 標準キーボード出現時
[AnyHashable("UIKeyboardCenterBeginUserInfoKey"): NSPoint: {384, 1180.5}, AnyHashable("UIKeyboardIsLocalUserInfoKey"): 1, AnyHashable("UIKeyboardCenterEndUserInfoKey"): NSPoint: {384, 867.5}, AnyHashable("UIKeyboardBoundsUserInfoKey"): NSRect: {{0, 0}, {768, 313}}, AnyHashable("UIKeyboardFrameEndUserInfoKey"): NSRect: {{0, 711}, {768, 313}}, AnyHashable("UIKeyboardAnimationCurveUserInfoKey"): 7, AnyHashable("UIKeyboardFrameBeginUserInfoKey"): NSRect: {{0, 1024}, {768, 313}}, AnyHashable("UIKeyboardAnimationDurationUserInfoKey"): 0.25]
値を見ると、userInfo["UIKeyboardCenterEndUserInfoKey"]
のy値で値で場合分けすればいいように思いますが、duprecated...
なので、userInfo["UIKeyboardFrameEndUserInfoKey"]
の微妙な高さの違いで判別するしかないのかなあ。
自分は今回、基準となる入力フォームに対してキーボードが重なっているかどうか程度の判別で良かったので、キーボードのフレームとフォームのフレームの大小で判定しました。