如果uitextfield是一个子视图
我对uitextfield
有一个奇怪的问题:
class AnnotateTextField: UIView {
// MARK: - Views
let textField = UITextField()
// MARK: - Initializers
init() {
super.init(frame: .zero)
setupViews()
}
@available(*, unavailable)
private override init(frame: CGRect) {
fatalError("init(frame:) has not been implemented")
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Setup
private func setupViews() {
let textField = UITextField()
textField.font = UIFont.systemFont(ofSize: 100.0)
textField.textColor = .white
backgroundColor = .black.withAlphaComponent(0.7)
clipsToBounds = true
layer.cornerRadius = 16.0
// Paint stays behind it
layer.zPosition = 1
addSubview(textField)
textField.snp.makeConstraints { make in
make.edges.equalToSuperview().inset(16.0)
}
}
}
如果我将其添加到我的视图中,并调用supsfirstresponder
键盘不会显示:
private func addText() {
let annotateTextField = AnnotateTextField()
addSubview(annotateTextField)
annotateTextField.snp.makeConstraints { make in
make.center.equalToSuperview()
}
annotateTextField.textField.becomeFirstResponder()
}
但是对于常规uite> uitexfield 它可以正常工作:
private func addText() {
let text = UITextField()
addSubview(text)
// This shows the keyboard
text.becomeFirstResponder()
}
我在这里做错了什么?我尝试在annotateTextfield
上设置用户交互设置,但没有区别。
I am having a strange issue with UITextField
:
class AnnotateTextField: UIView {
// MARK: - Views
let textField = UITextField()
// MARK: - Initializers
init() {
super.init(frame: .zero)
setupViews()
}
@available(*, unavailable)
private override init(frame: CGRect) {
fatalError("init(frame:) has not been implemented")
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Setup
private func setupViews() {
let textField = UITextField()
textField.font = UIFont.systemFont(ofSize: 100.0)
textField.textColor = .white
backgroundColor = .black.withAlphaComponent(0.7)
clipsToBounds = true
layer.cornerRadius = 16.0
// Paint stays behind it
layer.zPosition = 1
addSubview(textField)
textField.snp.makeConstraints { make in
make.edges.equalToSuperview().inset(16.0)
}
}
}
If I add this to my view and call becomesFirstResponder
the keyboard won't show:
private func addText() {
let annotateTextField = AnnotateTextField()
addSubview(annotateTextField)
annotateTextField.snp.makeConstraints { make in
make.center.equalToSuperview()
}
annotateTextField.textField.becomeFirstResponder()
}
However for a regular UITextField
it works fine:
private func addText() {
let text = UITextField()
addSubview(text)
// This shows the keyboard
text.becomeFirstResponder()
}
What am I doing wrong here? I tried setting user interaction enabled on AnnotateTextField
and the didn't make a difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为PREMLEM在
setupViews
函数中。您正在该功能中创建一个新的TextField对象,并在此TextField上进行更改,并将其添加到视图中。解决此问题删除函数内部函数中的新的Texfield对象,
或将类中的Textfield对象平衡到您在此函数中创建的对象如
I think the promlem is in
setupViews
function. You are creating a new textfield object in that function and you make changes on this textfield and add it to the view.To solve this problem delete new texfield object inside function like :
or equalizing the textfield object in the class to the object you created in this function like