我有这个文本域
let usn_text_field: LeftPaddedTextField = {
let tf = LeftPaddedTextField()
tf.placeholder = "Username"
tf.translatesAutoresizingMaskIntoConstraints = false
return tf
}()
usn_text_field.topAnchor.constraint(equalTo: info_centill.bottomAnchor).isActive = true
usn_text_field.leftAnchor.constraint(equalTo: view.leftAnchor,constant:20).isActive = true
usn_text_field.rightAnchor.constraint(equalTo: view.rightAnchor,constant:-75).isActive = true
我想为此tf添加边框图层,她是我的代码
let borderLayer = CALayer()
borderLayer.frame = CGRect(x:-5,y:39,width:usn_text_field.frame.width,height:1)
borderLayer.backgroundColor = UIColor(white: 0.7, alpha: 0.6).cgColor
usn_text_field.layer.addSublayer(borderLayer)
但是这会将usn_text_field.frame.width
返回为0.如果没有设置宽度,我可以获得frame.width
,还是有其他方法可以获得元素的宽度?
答案 0 :(得分:2)
添加layoutIfNeeded
为我工作