如何创建带下划线的UITextField [英] How to create underlined UITextField
问题描述
iOS noob
当我向ViewController添加默认 UITextField
时,它看起来像这样:
When I add a default UITextField
to the ViewController, it looks like this:
我的设计师希望我让 UITextField
看起来像这样(即背景是透明的,只是有下划线:
My designer would like me to make the UITextField
look like this (i.e. background is transparent, and just has an underline:
我该怎么做?应该要求我的设计师将图像设置为 UITextField
的背景。应该怎么做?图像看起来像?图像应该是=蓝色背景+下划线(减去我猜的数字)
How do I do this? Should be asking my designer for an image to set as background for the UITextField
. What should the image look like? Should the image be = the blue background + the underline (minus the number i guess)
是吗?
更新:
根据以下方向(来自@Ashish Kakkad),将此代码添加到Swift中以添加到ViewDidLoad:
UPDATE: 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
tf_editPassword.layer.addSublayer(bottomLine)
这几乎可行,但问题是所有边框都消失了,我想要保持底线。
This almost works, but the problem is that all side borders disappear, I want just the bottom line to stay.
推荐答案
@Ashish Kakkad发布了正确的代码,但它可能不适用于@ user1406716的原因是因为没有文本字段中的文本,框架的高度和宽度为0,因此您可以尝试以下操作:
@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
myTextField.layer.addSublayer(bottomLine)
这将即使文本字段中没有文本,也要确保该行可见。
This will ensure that the line is visible even when there is no text in the textfield.
这篇关于如何创建带下划线的UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!