【问题标题】:how to fix UITextfield Anchor programmatically?如何以编程方式修复 UITextfield Anchor?
【发布时间】:2019-06-01 16:34:58
【问题描述】:

有没有办法以编程方式设置UITextfield的锚点约束?

【问题讨论】:

  • 我做了,但它的工作方式与 uitextview 的工作方式不同
  • 哦,好的,请稍等
  • 我明白了!我使用乘数作为宽度而不是常数,它起作用了
  • 如果您使用发布的代码解决了问题,您应该删除您的问题或在下方发布答案,显示您为解决问题所做的工作。

标签: swift uitextfield constraints anchor


【解决方案1】:

这就是我让它工作的方式:

import UIKit

class ViewController: UIViewController,UITextFieldDelegate {
    lazy var passwordText: UITextField = {
        let textField = UITextField()
        let attributedText = NSMutableAttributedString(string: "Password", attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20),NSAttributedString.Key.foregroundColor: UIColor.gray])
        textField.backgroundColor = UIColor.clear
        textField.attributedText = attributedText
        textField.translatesAutoresizingMaskIntoConstraints = false
        textField.resignFirstResponder()
        textField.borderStyle = UITextField.BorderStyle.roundedRect
        return textField
    }()

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return false
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        passwordText.delegate = self
        view.addSubview(passwordText)
        passwordText.translatesAutoresizingMaskIntoConstraints = false
        passwordText.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor,constant:200).isActive = true
        passwordText.widthAnchor.constraint(equalTo: view.safeAreaLayoutGuide.widthAnchor,multiplier:0.5).isActive = true
        passwordText.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor,constant:30).isActive = true

    }
}

【讨论】:

  • 不清楚您对问题中的代码进行了哪些更改。请更新此答案以明确您修复了什么。
  • 我可以使用上面的代码调整我的 uitextfield 的锚点 ^
  • 当然可以,但是与问题中的代码相比,您做了哪些更改?为什么要从问题中删除无效代码?
  • 我真的不记得了。我改变了很多行代码,我终于工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-23
  • 2011-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多