我正在尝试创建只有底部边框的文本字段。该字段位于动态单元格内,并使用故事板创建。边框样式设置为“无”。自动布局已开启。该行在layoutSubviews()
中绘制。但是,该行不会出现。你能帮我解决一下吗? Thx提前!
以下是代码:
extension UITextField {
func drawUnderline() {
let underline = CALayer()
let underlineWidth = CGFloat(1.0)
underline.borderColor = UIColor.black.cgColor
underline.frame = CGRect(x: 0, y: self.frame.size.height - underlineWidth, width: self.frame.size.width, height: self.frame.size.height)
underline.borderWidth = underlineWidth
self.layer.addSublayer(underline)
self.layer.masksToBounds = true
}
}
class TextFieldTableViewCell: UITableViewCell {
@IBOutlet weak var editTextField: UITextField!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
override func layoutSubviews() {
super.layoutSubviews()
self.editTextField.drawUnderline() // The line is drawn here
}
}
答案 0 :(得分:2)
您可以从Interface Builder中执行此操作。创建固定高度= 1的UIView并给出一些颜色(黑色),确保与文本字段的宽度相等,只需将没有边框的UITextField连接到该视图。所以垂直空间textfield和view = 0之间的约束