Change custom text field background color and text color IOS Swift
Add self as a target for the UIControl events you need according to documentation
There you have control events for EditingDidBegin and such.
Something like this:
self.addTarget(self, action: "myFunc", forControlEvents: UIControlEvents.EditingDidBegin);
Select the text field -> click "identity inspector" icon -> click on Plus button "user Defined runtime attributes" -> add this text "_placeholderLabel.textColor" in "key path" field -> choose the "Type" "color" and set the value color.
In your CustomTextField
class you can add a property observer:
var change: Bool = false {
didSet {
textColor = change ? .yellow : .black
backgroundColor = change ? .blue : .white
}
}
and in your ViewController:
func textFieldDidBeginEditing(textField: UITextField) {
customTextField.change = true
}
func textFieldDidEndEditing(textField: UITextField) {
customTextField.change = false
}
Don't forget to set the delegate of your textfield, in storyboard or programmatically.
EDIT:
Shortened the code and updated for Swift 3