Dismiss a UIKeyboardTypeNumberPad in a UITextField

2019-08-14 18:24发布

问题:

How do I dismiss a UITextField with keyboard type UIKeyboardTypeNumberPad? The accepted answer to this question suggests to either add a bar with a dismiss button as an inputAccessoryView or to listen to touch events on the background.

I prefer the second approach, i.e. listen to touch events on the view containing the UITextField. However, this approach fails if VoiceOver is active. How do you resignFirstResponder in that situation?

回答1:

You need an alternative way to perform the gesture. You'll have to add a button to your layout. Sorry...

You should be able to check whether VoiceOver is on or not.

BOOL UIAccessibilityIsVoiceOverRunning();

You could also consider adding a tap recognizer to an invisible UI Element or a parent View that is completely covered with other elements. In this way, it would only be able to be activated by a VoiceOver user.