Created
December 30, 2019 23:23
-
-
Save shanecowherd/02f86757f9340c5a268f956e82e1409a to your computer and use it in GitHub Desktop.
This adds a "done" button to your keyboard toolbar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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