// // UITextFieldWithDoneButton.swift // Learn Genie // // Created by Suraj Kumar Mandal on 17/09/21. // import Foundation import UIKit class UITextFieldWithDoneButton: UITextField { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.addDoneButtonOnKeyboard() } fileprivate 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 fileprivate func doneButtonAction() { self.resignFirstResponder() } }