总有办法以编程方式设置UITextfield
的锚的约束吗?
答案 0 :(得分:-2)
这就是我的工作方式:
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
}
}