Swift 如何以编程方式修复UITextfield锚?
是否可以通过编程方式设置UITextfield的锚的约束?我就是这样让它工作的:Swift 如何以编程方式修复UITextfield锚?,swift,uitextfield,constraints,anchor,Swift,Uitextfield,Constraints,Anchor,是否可以通过编程方式设置UITextfield的锚的约束?我就是这样让它工作的: import UIKit class ViewController: UIViewController,UITextFieldDelegate { lazy var passwordText: UITextField = { let textField = UITextField() let attributedText = NSMutableAttributedStrin
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
}
}
我做到了,但它的工作方式与uitextview不同哦,好吧,给我一分钟,我明白了!我使用了宽度乘数而不是常数,它起作用了如果你用你发布的代码解决了问题,你应该删除你的问题,或者在下面发布一个答案,说明你是如何解决问题的。现在还不清楚你对问题中的代码做了哪些更改。请更新此答案以明确您修复了什么。我可以使用上面的代码调整uitextfield的锚定^?为什么要从问题中删除非工作代码?我真的不记得了。我改变了太多的代码行,最终我成功了。