Created
July 7, 2017 09:38
-
-
Save DejanEnspyra/44892915ba9582eec147c98e00ea4335 to your computer and use it in GitHub Desktop.
Add keyboard Done button using UIToolbar in Swift 3 (http://theappspace.com/swift-add-keyboard-done-button/)
This file contains hidden or 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
| import Foundation | |
| import UIKit | |
| extension UITextField{ | |
| @IBInspectable var doneAccessory: Bool{ | |
| get{ | |
| return self.doneAccessory | |
| } | |
| set (hasDone) { | |
| if hasDone{ | |
| addDoneButtonOnKeyboard() | |
| } | |
| } | |
| } | |
| func addDoneButtonOnKeyboard() | |
| { | |
| let doneToolbar: UIToolbar = UIToolbar(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50)) | |
| doneToolbar.barStyle = .default | |
| let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) | |
| let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction)) | |
| let items = [flexSpace, done] | |
| doneToolbar.items = items | |
| doneToolbar.sizeToFit() | |
| self.inputAccessoryView = doneToolbar | |
| } | |
| func doneButtonAction() | |
| { | |
| self.resignFirstResponder() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks man,