如何在Swift中只显示UITextField的底部边框 [英] How to only show bottom border of UITextField in Swift

查看:553
本文介绍了如何在Swift中只显示UITextField的底部边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示底部边框并隐藏其他边。

I want to show only bottom border and hide the other sides.

输出我看到:正如您所见,我看到顶部,左右边框也是黑色,我想删除它们。只需要底部白色厚2.0边框。

Output I see: As you can see I see the top, left and right borders also and they are black in color, I want to remove them. Only need the bottom white thick 2.0 border.

我正在使用的代码(来源):

Code I am using (source):

var border = CALayer()
var width = CGFloat(2.0)
border.borderColor = UIColor.whiteColor().CGColor
border.frame = CGRect(x: 0, y: tv_username.frame.size.height - width, width: tv_username.frame.size.width, height: tv_username.frame.size.height)

border.borderWidth = width
tv_username.backgroundColor = UIColor.clearColor()
tv_username.layer.addSublayer(border)
tv_username.layer.masksToBounds = true
tv_username.textColor = UIColor.whiteColor()


推荐答案

尝试这样做。

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

你必须设置 borderStyle 属性为

如果您使用自动布局,则设置完美约束,否则不会出现底线。

If you are using the autolayout then set perfect constraint else bottomline will not appear.

希望有所帮助。

这篇关于如何在Swift中只显示UITextField的底部边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆