Changing textField border color iOS

user2017285 picture user2017285 · Mar 1, 2013 · Viewed 42.9k times · Source

I want to change UITextField border color. Is it possible to customize the color of border? I searched all options in xib but I did not found any option to change border color.

Answer

KDeogharkar picture KDeogharkar · Mar 1, 2013

you can use this:

yourTextField.layer.borderColor=[[UIColor blackColor]CGColor];

yourTextField.layer.borderWidth=1.0;

and remember to import #import <QuartzCore/QuartzCore.h> in your .h file

You can also specify your RGB value.

yourTextField.layer.borderColor=[[UIColor colorWithRed:178.0f/255.0f green:178.0f/255.0f blue:178.0f/255.0f alpha:1.0] CGColor];

Note: you need to set both values starting with iOS 7

Update for swift 2.3

yourTextField.layer.borderColor = UIColor.blackColor().CGColor
yourTextField.layer.borderWidth = 1.0

OR

yourTextField.layer.borderColor = UIColor(red: 178.0 / 255.0, green: 178.0 / 255.0, blue: 178.0 / 255.0, alpha: 1.0).CGColor

Update for swift 3.1.1

yourTextField.layer.borderColor = UIColor.black.cgColor
yourTextField.layer.borderWidth = 1.0

OR

yourTextField.layer.borderColor = UIColor(red: 178.0 / 255.0, green: 178.0 / 255.0, blue: 178.0 / 255.0, alpha: 1.0).cgColor