ioskeyboardresignfirstresponder

Is it possible to add a Done key to ALL keyboards in an app?


Is it possible to add a done or cancel key which dismisses a keyboard to all keyboards in an iOS app? There are several posts asking how to dismiss a keyboard via a cancel or done button, but the solutions are on a field by field basis.

Are there any solutions that add this functionality globally so the code wont need to be duplicated for each textfield/area in an application?


Solution

  • Like @silentBob says in his answer, the inputAccessoryView of a text field is the view that’s displayed immediately above the keyboard when the text field is the first responder. If you didn’t want to write an extension on UITextField to override the -inputAccessoryView method, you could also create a subclass of UITextField to do the same, which would make it easier to determine which method is going to be called. You could also have multiple subclasses of UITextField to customize which button(s) appear. If you have a text field in a storyboard, you can simply change the class to your custom subclass, so while you have to go through and make those changes, you don’t have to do it in code.