I made it work, but have met a few problems:
I implemented simple actions:
@objc func pickerDoneTapped() {
print("Tapped")
}
@objc func pickerCancelTapped() {
print("Cancelled")
}
They never got called.
That was because the button targets was self, ie the tabBar, not the vc
So, I changed adding a vc parameter:
func addToolBarHandler(selectorDone : Selector , selectorCancel : Selector, vc: UIViewController ) -> UIToolbar {
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: vc, action: selectorDone)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItem.Style.plain, target: vc, action: selectorCancel)
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
toolBar.sizeToFit()
return toolBar
}
called as:
let toolBar = UIToolbar.init().addToolBarHandler(selectorDone: #selector(pickerDoneTapped), selectorCancel: #selector(pickerCancelTapped), vc: self)
Which worked.
I could not repeat the problem of keyboard being shown instead of picker.
You should search if inputView could be set to nil in some cases.
var inputView: UIView? { get set }
If the value in this property is
nil
, the text field displays the standard system keyboard when it becomes first responder. Assigning a custom view to this property causes that view to be presented instead.
May be you could test self.accountPickerView for nil in func textFieldDidBeginEditing(_ textField: UITextField)
EDITED:
I would try the following: assign the inputView to textField and not to the activeTextField
textField.inputAccessoryView = toolBar
if (self.activeTextField == self.selectAccountTB ) {
textField.inputView = self.accountPickerView
}
or even to ith IBOutlet directly:
self.selectAccountTB.inputAccessoryView = toolBar
if (self.activeTextField == self.selectAccountTB ) {
self.selectAccountTB.inputView = self.accountPickerView
}
Or, even better probably:
set the input View and accesssoryView for the 3 textField right in viewDidload, not in textDidBeginEditing.
I could be that it is too late there in some cases and keybaord is already set as inputView.