My app uses a UIAccessoryView
to provide additional keyboard functionality (such as forward/backward tabs and arrows keys) for the virtual keyboard, but that causes UIKeyboardDidShowNotification to fire even when a physical keyboard is present (the accessory appears at the bottom of the screen).
I'd like to check if a physical keyboard is attached when handling UIKeyboardWillShowNotification, to prevent the accessory view from appearing and to prevent my custom view from scrolling up (to make room for the non-existent virtual keyboard).
I've tried examining the UIKeyboardFrameEndUserInfoKey
key, but it returns a real size for the virtual keyboard, in spite of nothing being displayed.
Is there any way to detect the presence of a physical keyboard to prevent this unwanted behaviour?
Hmm, the plot thickens.
I tried disabling the input accessory by returning nil from the inputAccessoryView property of the Responder object which triggers the keyboard. That suppresses UIKeyboardWillShowNotification
and UIKeyboardDidShowNotification
when there is a physical keyboard present, but keeps these notifications when there is no such keyboard. All good so far.
Then I tried re-enabling inputAccessoryView only after UIKeyboardWillShowNotification
had been received. This only fires when a virtual keyboard is needed, so it should allow me to reintroduce the accessory view in those circumstances. Or so I thought.
Unfortunately, it seems the OS doesn't check inputAccessoryView after UIKeyboardWillShowNotification
, so it fails to show the accessory view when it is needed :o(
That leaves me with two options:
Not a great choice, so I'm still keen to see if anyone else has addressed this problem!
(This answer was suggested by Sean Heber on the Apple forums)
When you get a UIKeyboardDidShowNotification
, instead of using the frame given by UIKeyboardFrameEndUserInfoKey
, simply test where the view's accessory view is and use that, instead. All I do is check to see what the frame of the accessory view is and convert it to my own view's coordinate space. Then I can easily use that frame to decide what to do.
Note: When the on-screen keyboard is shown/hidden using the bluetooth keyboard's eject button, it seems that UIKit sends UIKeyboardDidShowNotification
again but not a UIKeyboardWillHideNotification
. This is also a confusing inconsistency, but the above workaround is still valid as the system will change the frame of the inputAccessoryView when it slides in the on-screen keyboard, so you can still adjust things accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With