我的应用程序中有一个自定义UITextField,
class UnderLinedText: UITextField {
override func layoutSubviews() {
super.layoutSubviews()
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = UIColor(red:0.21, green:0.13, blue:0.34, alpha:1.00).cgColor
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: self.frame.size.height)
border.borderWidth = width
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
override init(frame: CGRect) {
super.init(frame: frame)
self.textAlignment = .center
self.borderStyle = .none
self.isEnabled = true
self.isUserInteractionEnabled = true
self.font = UIFont(name: "HelveticaNeueLT-Regular", size: 20)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我将其添加到我的视图控制器中,但无法正常工作(无法对其进行文本编辑)
class CommercialOrder: UIViewController , UITextFieldDelegate{
let bid = UnderLinedText()
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
bid.delegate = self
}
func updateUI(){
self.view.backgroundColor = .white
self.view.addSubview(scrollView)
scrollView.addSubview(contentView)
scrollView.showsVerticalScrollIndicator = false
scrollView.autoPinEdgesToSuperviewSafeArea()
contentView.autoPinEdgesToSuperviewEdges()
contentView.autoMatch(.width, to: .width, of: view)
contentView.addSubview(header)
contentView.addSubview(bid_title)
bid.autoPinEdge(.top, to: .bottom, of: bid_title, withOffset: 10)
bid.autoPinEdge(toSuperviewEdge: .trailing, withInset: 20)
bid.autoPinEdge(toSuperviewEdge: .leading, withInset: 20)
bid.autoSetDimension(.height, toSize: 50)
bid.text = "Reciever mail"
}
我什至还添加了 UITextFieldDelegate ,将 isEnabled 和 isUserInteractionEnabled 设置为true,它仍然不起作用
任何帮助将不胜感激
答案 0 :(得分:0)
希望能帮助别人,我的问题是contentView没有高度,这会影响滚动(视图不可滚动),因此它不会发生触摸事件,只需将高度或约束添加到contentView的最后一个视图的底部就像我的情况下一样
button.autoPinEdge(toSuperviewEdge: .bottom, withInset: 30)
就这样