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
.
Asked
Active
Viewed 4.5k times
24

Mudriy Ilya
- 122
- 2
- 11

user2017285
- 283
- 1
- 2
- 10
-
3sorry when i searched for textfield border color change i dint found that post so i posted otherwise i would have not. – user2017285 Mar 01 '13 at 09:15
3 Answers
49
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

KDeogharkar
- 10,939
- 7
- 51
- 95
-
3
-
2It is only work with borderWidth . you have to give borderWidth even in iOS6. It is working fine with iOS7 also. – KDeogharkar Mar 20 '14 at 04:03
-
Struggled with this about 10mins! Finally it appears that I've not connected textField to the outlet ( – trickster77777 Oct 30 '14 at 16:23
-
1
-
It might be worth noting in the answer, that you need to set both the `borderColor` and `borderWith` in iOS 7 and iOS 8 to make the color actually change. – Markus Rautopuro Nov 29 '14 at 09:25
-
markField.layer.borderColor = UIColor.clear.cgColor; - for Swift 5 – Slarty Bartfast Jul 11 '20 at 13:10
12
#import <QuartzCore/QuartzCore.h>
Use below code to change Textfield's border color
textField.layer.borderWidth = 2.0f;
textField.layer.borderColor = [[UIColor redColor] CGColor];
textField.layer.cornerRadius = 5;
textField.clipsToBounds = YES;
For SWIFT:
textField.layer.borderColor = UIColor.redColor().CGColor;

Nazir
- 1,945
- 24
- 27
5
Use Quartzcore framework. You can set the textField.layer.borderWidth, as well as borderColor:
tField.layer.borderColor = [UIColor redColor].CGColor;
for more reference: https://stackoverflow.com/a/5749376/1554632