【发布时间】:2020-06-09 19:40:56
【问题描述】:
所以,我有这个自定义 UITextField,我有两种方法可以添加 CALayer 和删除 CALayer,但删除不起作用。
@IBDesignable class AppTextField : UITextField {
private let bottomLine = CALayer()
override func layoutSubviews() {
self.font = .systemFont(ofSize: 20)
self.addBottomLine()
self.clearButtonMode = .unlessEditing
super.layoutSubviews()
}
func removeBttomLine() {
bottomLine.removeFromSuperlayer()
}
private func addBottomLine() {
bottomLine.frame = CGRect(origin: CGPoint(x: 0, y: self.frame.height + 4), size: CGSize(width: self.frame.width, height: 1))
bottomLine.backgroundColor = UIColor.init(hexString: "#DCCFCA")?.cgColor
self.borderStyle = .none
self.layer.addSublayer(bottomLine)
}
}
【问题讨论】:
-
你在哪里打电话
removeBttomLine()? -
@DonMag 在我的
viewController. -
你是否在开头显示红线?然后在该字段成为第一响应者时将其删除?或者不显示它直到它成为第一响应者?
-
@DonMag 我不确定!我正在尝试从我的
ViewController的ViewDidLoad中删除它。