Skip to content

Instantly share code, notes, and snippets.

@shanecowherd
Created December 30, 2019 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanecowherd/02f86757f9340c5a268f956e82e1409a to your computer and use it in GitHub Desktop.
Save shanecowherd/02f86757f9340c5a268f956e82e1409a to your computer and use it in GitHub Desktop.
This adds a "done" button to your keyboard toolbar.
//Modified from:
//http://swiftquickstart.blogspot.com/2016/10/adding-done-button-to-keyboard.html
extension UITextField {
func addKeyboardDoneButton() {
let keyboardToolBar = UIToolbar()
keyboardToolBar.sizeToFit()
let flexibleSpace = UIBarButtonItem(barButtonSystemItem:
UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem:
UIBarButtonItem.SystemItem.done, target: self, action: #selector(self.donePressed) )
keyboardToolBar.setItems([flexibleSpace, doneButton], animated: true)
self.inputAccessoryView = keyboardToolBar
}
@objc private func donePressed() {
self.endEditing(true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment