Custom UITextField appearance?

I'm a big stuck with UITextFields at the moment.


I'm trying to implement a custom appearance for the Text Fields in my app, however they don't seem to work as intended. As an example, I put three text fields on a viewcontroller, each using my CustomTextField class. There is additional logic in my original files to change other aspects of the appearance, but that'll do it:


let bgColor = UIColor.darkGrayColor()
let focusedBgColor = UIColor.whiteColor()

class CustomTextField: UITextField {
    override func awakeFromNib() {
        super.awakeFromNib()
       
        self.backgroundColor = bgColor
    }
   
    override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
        coordinator.addCoordinatedAnimations({
            if self.focused {
                self.backgroundColor = focusedBgColor
                self.transform = CGAffineTransformMakeScale(1.1, 1.1)
            } else {
                self.backgroundColor = bgColor
                self.transform = CGAffineTransformIdentity
            }
        }, completion: nil)
    }
}


Everything seems to work fine when just switching focus between the text fields.


However, when I actually enter the text fields (show keyboard, enter something (or not)) and go back using the menu button (or the done button), the text field I initially clicked on will change its appearance to something... different. It'll still be in its focused scaling state and the background will become a mix of the "focused" backgroundColor and the "normal" background Color.


I'm confused. Am I doing something wrong or is this actually a bug?

I'm not sure if it's relevant to your issue, since I'm not using a UITextField subclass, but I have found that the textColor that I set for UITextFields in a TV app gets reset as soon as the text field is focused. I'm using a white background with blue text and when the text field is focused, the text turns black. When the focus leaves the text field, the text becomes a shade of gray. It doesn't matter if I set the text color in the Storyboard or programatically. Even programmatically changing the text color after focus leaves the text field doesn't work.

Have you found a fix for this?

I'm also dealing with this issue. Any known workarounds?

I found a workaround: I set my text field's appearance in a dispatch_async (on the main thread) called inside of the didUpdateFocusInContext method.

Custom UITextField appearance?
 
 
Q