I am creating a custom subclass from UITextField class. I want to apply something while textfield is focused. But in my custom class my delegate methods are not calling.
I have create a subclass that extends UITextField class and make some customisation.
In TGTextField class:
class TGTextField: UITextField, UITextFieldDelegate {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
delegate = self
createBorder()
}
required override init(frame: CGRect) {
super.init(frame: frame)
delegate = self
createBorder()
}
func createBorder(){
self.layer.borderColor = AppColor.TextFieldColors.BorderNormalColor.cgColor
self.layer.borderWidth = 1.0;
self.layer.cornerRadius = 8
}
func textFieldDidBeginEditing(textField: UITextField) {
print("focused")
self.activeTextFieldColor()
}
func textFieldDidEndEditing(textField: UITextField) {
print("lost focus")
self.deactiveTextFieldColor()
}
func activeTextFieldColor(){
self.layer.borderColor = AppColor.TextFieldColors.BorderActiveColor.cgColor
}
func deactiveTextFieldColor(){
self.layer.borderColor = AppColor.TextFieldColors.BorderNormalColor.cgColor
}
}
Both these delegates methods are not called.
func textFieldDidBeginEditing(textField: UITextField) {
print("focused")
self.activeTextFieldColor()
}
func textFieldDidEndEditing(textField: UITextField) {
print("lost focus")
self.deactiveTextFieldColor()
}