Skip to content

Instantly share code, notes, and snippets.

@shanecowherd
Created December 30, 2019 23:23
Embed
What would you like to do?
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