如何创建具有透明背景且仅具有底部边框的textField?
我已经尝试过以下代码:
textField.backgroundColor = .clear
let border = CALayer()
let width = CGFloat(2.0)
border.borderColor = UIColor.darkGrayColor().CGColor
border.frame = CGRect(x: 0, y: textField.frame.size.height - width, width: textField.frame.size.width, height: textField.frame.size.height)
border.borderWidth = width
textField.layer.addSublayer(border)
textField.layer.masksToBounds = true
但它不起作用。
发布于 2017-01-31 12:46:11
下面是一个使用UIView而不是CALayer的替代实现。
let line = UIView()
line.frame.size = CGSize(width: textField.frame.size.width, height: 1)
line.frame.origin = CGPoint(x: 0, y: textField.frame.maxY - line.frame.height)
line.backgroundColor = UIColor.darkGray
line.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
textField.addSubview(line)
发布于 2017-01-31 12:41:44
您的代码是正确的。问题只是你的边框高度现在是"textField.frame.size.height“,把它改成1.0。(改变你的边框代码)。
发布于 2017-01-31 12:48:08
请尝试以下操作:
txtField .borderStyle = .none
它可能会帮助你..。:)
https://stackoverflow.com/questions/41949672
复制相似问题