[英]How to create underlined UITextField
iOS noob here iOS 菜鸟在这里
When I add a default UITextField
to the ViewController, it looks like this:当我向 ViewController 添加一个默认的UITextField
,它看起来像这样:
My designer would like me to make the UITextField
look like this (ie 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
.应该要求我的设计师将图像设置为UITextField
背景。 What should the image look like?图像应该是什么样的? Should the image be = the blue background + the underline (minus the number i guess)图像应该是 = 蓝色背景 + 下划线(减去我猜的数字)
Is that right?那正确吗?
UPDATE : Based on direction below (from @Ashish Kakkad), added this code in Swift to ViewDidLoad:更新:根据以下方向(来自@Ashish Kakkad),将此代码在 Swift 中添加到 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.这几乎有效,但问题是所有边边框都消失了,我只想保留底线。
Try this code :试试这个代码:
Obj-C对象-C
CALayer *bottomLine = [CALayer layer];
bottomLine.frame = CGRectMake(0.0f, self.frame.size.height - 1, self.frame.size.width, 1.0f);
bottomLine.backgroundColor = [UIColor whiteColor].CGColor;
[myTextField setBorderStyle:UITextBorderStyleNone];
[myTextField.layer addSublayer:bottomLine];
Swift迅速
var bottomLine = CALayer()
bottomLine.frame = CGRectMake(0.0, view.frame.height - 1, view.frame.width, 1.0)
bottomLine.backgroundColor = UIColor.whiteColor().CGColor
myTextField.borderStyle = UITextBorderStyle.None
myTextField.layer.addSublayer(bottomLine)
Hope it helps希望能帮助到你
If you are using the auto-layout then try to do this code inside the viewDidLayoutSubviews
method.如果您正在使用自动布局,请尝试在viewDidLayoutSubviews
方法中执行此代码。
@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 : @Ashish Kakkad 发布了正确的代码,但它可能不适用于 @user1406716 的原因是因为当文本字段中没有文本时,框架的高度和宽度为 0,因此您可以尝试以下操作:
Obj-C :对象-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.这将确保即使文本字段中没有文本,该行也是可见的。
Use this Extension for UITextField将此扩展用于 UITextField
extension UITextField {
func setUnderLine() {
let border = CALayer()
let width = CGFloat(0.5)
border.borderColor = UIColor.darkGray.cgColor
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width - 10, height: self.frame.size.height)
border.borderWidth = width
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}
Call the above function in ViewDidLoad()
and display the textfield with an underline.在ViewDidLoad()
调用上述函数并显示带有下划线的文本字段。
myTextField.setUnderLine()
To update this with Swift 3:要使用 Swift 3 更新它:
let bottomLine = CALayer()
bottomLine.frame = CGRect(origin: CGPoint(x: 0, y:first.frame.height - 1), size: CGSize(width: first.frame.width, height: 1))
bottomLine.backgroundColor = UIColor.white.cgColor
first.borderStyle = UITextBorderStyle.none
first.layer.addSublayer(bottomLine)
Delete borders and add underline - Swift 5:删除边框并添加下划线 - Swift 5:
extension UITextField {
func addUnderLine () {
let bottomLine = CALayer()
bottomLine.frame = CGRect(x: 0.0, y: self.bounds.height + 3, width: self.bounds.width, height: 1.5)
bottomLine.backgroundColor = UIColor.lightGray.cgColor
self.borderStyle = UITextField.BorderStyle.none
self.layer.addSublayer(bottomLine)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.