有些时候会觉得iOS默认的样式比较难看,而希望把文本框改成下划线那种形式,实现的代码其实很简单,大概用这样几行代码就可以了。
static func underline(textfield: UITextField, color: UIColor)
{
// 创建一条线,长度与文本框等长,宽度为2个像素
let underLine = UIView.init(frame: CGRect.init(x: 0, y: textfield.height - 2, width: textfield.width, height: 2))
// 定义下划线的颜色
underLine.backgroundColor = color
// 把自定义的组建加入文本框中
textfield.addSubview(underLine)
// 然后别忘了把文本框外框设置成none
textfield.borderStyle = .none
}
觉得好用,点个赞再走呗~~~