如何只保留 UITextField 的下边框?

How to leave only the lower border of UITextField?

需要在 UITextField 处只保留底部边框。出于某种原因,我的代码不起作用。

let bottomLine = CALayer()
bottomLine.frame = CGRect(x:0.0, y:1.0, width: myTextField.frame.width, 
height: myTextField.frame.height - 1)
bottomLine.backgroundColor = UIColor.white.cgColor
myTextField.borderStyle = UITextBorderStyle.None
myTextField.layer.addSublayer(bottomLine)

替换这个

 bottomLine.frame = CGRect(x:0.0, y:1.0, width: myTextField.frame.width,height: myTextField.frame.height - 1)

bottomLine.frame = CGRect(x:0.0, y:myTextField.frame.height - 1, width: myTextField.frame.width,height: 1)

在我的案例中添加:

myTextFeld.wantsLayer = true