我试图为UITextField
添加一条底线。我做错了什么
let emialTextField: UITextField = {
let textField = UITextField()
textField.layer.cornerRadius = 5
textField.borderStyle = .none
textField.placeholder = "emial adress"
let bottomline = CALayer()
bottomline.frame = CGRect(x: 0, y: textField.frame.height - 2, width: textField.frame.width, height: 2)
bottomline.backgroundColor = UIColor.init(red: 0/255, green: 0/255, blue: 0/255, alpha: 1).cgColor
textField.layer.addSublayer(bottomline)
return textField
}()
2条答案
按热度按时间vatpfxk51#
试试下面的代码。
j2cgzkjk2#
在执行下面的代码时,
textField.frame.width
似乎为零:因此,您应该通过约束或帧大小为
emialTextField
设置大小后,将bottomline
层添加到文本字段。