Add done button on top of keyboard swift

Add done button on top of keyboard swift

Learn how to add a done button above the keyboard so when it is clicked it will close/dismiss the keyboard. Collectionview inside ScrollView :-    • Collectionview inside Scrollview   Dynamic height TableView in a Scroll View :-    • Dynamic height TableView in a Scroll View   Simple add this extension to your project. 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 } @objc func doneButtonAction() { self.resignFirstResponder() } } /////////// USE ////////////// your_text_field.addDoneButtonOnKeyboard()