Below my question is the code i'm using in xCode 10 to place a "Done" button on the pop up keyboard. I have a second ViewController but don't know how to use this code on this second one. The errors displayed in my second ViewController are:
Invalid redeclaration of 'doneAccessory'
Invalid redeclaration of 'addDoneButtonOnKeyboard()'
Invalid redeclaration of 'doneButtonAction()'
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()
}
}
Please add this UITexfield extension to your helper class. Its good practice having all your extensions in the helper class.
You need not set anything about your second view controller. You need to open your storyboard and select the text field where you want the done button. Find the image to set the done button then you will not face any issue.