user1406716 user1406716 - 27 days ago 5x
Swift Question

how to get this (see inside) type of a UITextField

iOS noob here

When I add a default

to the ViewController, it looks like this:

enter image description here

My designer would like me to make the
look like this (i.e. background is transparent, and just has an underline:

enter image description here

How do I do this? Should be asking my designer for an image to set as background for the
. What should the image look like? Should the image be = the blue background + the underline (minus the number i guess)

Is that right?

Based on direction below (from @Ashish Kakkad), added this code in Swift to ViewDidLoad:

var bottomLine = CALayer()
bottomLine.frame = CGRectMake(0.0, view.frame.height - 1, view.frame.width, 1.0)
bottomLine.backgroundColor = UIColor.whiteColor().CGColor
tf_editPassword.borderStyle = UITextBorderStyle.None

This almost works, but the problem is that all side borders disappear, I want just the bottom line to stay.

Das Das

@Ashish Kakkad has posted the right code but the reason it might not be working for @user1406716 is because when there is no text in the textfield, the height and width of the frame are 0 so maybe you can try the following :

Obj-C :

CALayer *bottomLine = [CALayer layer];
bottomLine.frame = CGRectMake(0.0f, 75 - 1, 300, 1.0f);
bottomLine.backgroundColor = [UIColor whiteColor].CGColor;
[myTextField setBorderStyle:UITextBorderStyleNone];
[myTextField.layer addSublayer:bottomLine];

Swift :

var bottomLine = CALayer()
bottomLine.frame = CGRectMake(0.0, 75 - 1, 300, 1.0)
bottomLine.backgroundColor = UIColor.whiteColor().CGColor
myTextField.borderStyle = UITextBorderStyle.None

This will ensure that the line is visible even when there is no text in the textfield.