如果uitextfield是一个子视图

发布于 2025-02-03 06:26:03 字数 1715 浏览 3 评论 0原文

我对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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

垂暮老矣 2025-02-10 06:26:03

我认为PREMLEM在setupViews函数中。您正在该功能中创建一个新的TextField对象,并在此TextField上进行更改,并将其添加到视图中。

解决此问题删除函数内部函数中的新的Texfield对象,

 private func setupViews() {

    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)
    }
}

或将类中的Textfield对象平衡到您在此函数中创建的对象如

 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
    self.textField = textField // add this
    addSubview(self.textField) // change this
    textField.snp.makeConstraints { make in
        make.edges.equalToSuperview().inset(16.0)
    }
}

annotateTextfield.textfield的框架为零,因为您正在创建一个新的TextField,并且您正在设置函数中的Textfield

中的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 :

 private func setupViews() {

    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)
    }
}

or equalizing the textfield object in the class to the object you created in this function like

 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
    self.textField = textField // add this
    addSubview(self.textField) // change this
    textField.snp.makeConstraints { make in
        make.edges.equalToSuperview().inset(16.0)
    }
}

annotateTextField.textField's frame is zero because you are creating a new textfield and you are setting constraint to that textfield in function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文