1

I have a few textfields in my view controller, I created a custom class for them:

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.white.cgColor

    self.backgroundColor = UIColor.black
    self.textColor = UIColor.white
}
}

In my view controller, the outlets are created like this:

@IBOutlet weak var itemCountTextField: CustomTextField! { didSet { itemCountTextField.delegate = self } }

But when I ran it, the UITextFields still look like default white text fields, the customization isn't done, where did I do wrong?

Bright
  • 5,699
  • 2
  • 50
  • 72
  • I think you need set border style to other kind to get out of the default style also – Tj3n Sep 15 '16 at 07:48
  • @Tj3n I just tried, doesn't seem the case, plus it seems to work well in this question: http://stackoverflow.com/questions/29888253/change-custom-text-field-background-color-and-text-color-ios-swift – Bright Sep 15 '16 at 07:51

2 Answers2

1

You set the UITextField subclass is CustomTextField & then works...

enter image description here

Rahul Mayani
  • 3,761
  • 4
  • 25
  • 40
1

Make sure you set the textfield class to CustomTextField also in your Storyboard/Xib, just changing the outlet type is not enough.

Jelly
  • 4,522
  • 6
  • 26
  • 42