extension UIView {
open override func becomeFirstResponder() -> Bool {
if (self is UITextField || self is UITextView) && inputAccessoryView == nil {
let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: bounds.size.width, height: 25))
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneBtn = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(doneButtonAction))
toolbar.setItems([flexSpace, doneBtn], animated: false)
toolbar.sizeToFit()
(self is UITextField)
? ((self as! UITextField).inputAccessoryView = toolbar)
: ((self as! UITextView).inputAccessoryView = toolbar)
}
return super.becomeFirstResponder()
}
@objc private func doneButtonAction() {
UIApplication.shared.sendAction(#selector(UIApplication.resignFirstResponder), to: nil, from: nil, for: nil)
}
}