Change custom text field background color and text color IOS Swift

I’m using following custom text field class to change appearance of the text field. Now I need to change text field’s background color, text color and place holder color, when user start editing and end editing the text field. How to do it, using this class.

import Foundation
import UIKit

class CustomTextField: UITextField{

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.layer.cornerRadius = 15.0;
        self.layer.borderWidth = 1.5
        self.layer.borderColor = UIColor.whiteColor().CGColor

        self.backgroundColor = UIColor(white: 1, alpha: 0.0)

        self.textColor = UIColor.whiteColor()
        self.textAlignment = NSTextAlignment.Center


  • Error compiling tweak in theos
  • Uploading image with IOS app to server file size is too large
  • How to perform DNS query on iOS
  • Disable Single Sign On (SSO) in Facebook IOS SDK
  • Aspect-Oriented Objective-C Library?
  • How to change UITextfield placeholder color and fontsize using swift 2.0?
  • 2 Solutions Collect From Internet About “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);

    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.

    Shortened the code and updated for Swift 3