I was having the exact same issue (in Swift 3). I solved the problem overriding the delegate
property from the UITextField
class.
During my custom view initialization, I hook up my own internal delegate:
class CustomTextField: UITextField, UITextFieldDelegate {
override public init(frame: CGRect) {
super.init(frame: frame)
initCustomTextField()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initCustomTextField()
}
private func initCustomTextField() {
super.delegate = self // Note the super qualifier.
}
...
Now we need to override the aforementioned delegate
property:
private weak var userDelegate: UITextFieldDelegate?
override var delegate: UITextFieldDelegate? {
get { return userDelegate }
set { userDelegate = newValue }
}
Finally, on each UITextFieldDelegate
protocol method you must forward the call to the external delegate, if any:
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
// Do your thing here, and then forward:
return self.delegate?.textFieldShouldBeginEditing?(self) ?? true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
// Do your thing here, and then forward:
self.delegate?.textFieldDidBeginEditing?(self)
}
...
One caveat if you plan to support iOS 10 as well:
func textFieldDidEndEditing(_ textField: UITextField) {
self.delegate?.textFieldDidEndEditing?(self)
}
/// This method will be called, instead of the above, on iOS ≥ 10.
@available(iOS 10.0, *)
func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {
self.delegate?.textFieldDidEndEditing?(self, reason: reason)
}