0

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?

Community
  • 1
  • 1
propstm
  • 3,461
  • 5
  • 28
  • 41

3 Answers3

1

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.

Community
  • 1
  • 1
Jeff Kelley
  • 19,021
  • 6
  • 70
  • 80
0

Yes, you can add an extension to UITextField class, which should add a UIToolbar with Done and Cancel actions as UITextField inputAccessoryView.

silentBob
  • 170
  • 9
0

Well, In that case you have to customise the keyboard, built your own keyboard and do whatever you need to do with your key in the keyboard.

Abhishek Mitra
  • 3,335
  • 4
  • 25
  • 46