当我取消对CGRect的注释时,出现一个文本字段,但是如果我将其注释掉,则不会出现。如果使用CGrect,约束将无法使用。
let createTextField: TextFieldBounds = {
//var myTextField = TextFieldBounds(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
var myTextField = TextFieldBounds ()
myTextField.insetX = 45
//let myTextField = UITextField(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
myTextField.placeholder = "fillertext" //set placeholder text
myTextField.font = UIFont.systemFont(ofSize: 14) // set font size of text field
myTextField.layer.borderWidth = 1.0 //set width
myTextField.layer.borderColor = UIColor.red.cgColor//set background color to a ui color
myTextField.layer.backgroundColor = UIColor.white.cgColor
myTextField.layer.cornerRadius = myTextField.frame.height/2
myTextField.autocorrectionType = .no // disable autocorrect when typing for .no, enable with .yes
myTextField.isSecureTextEntry = true// masked text
myTextField.keyboardType = .default //keyboard style is set to default
myTextField.returnKeyType = .default //retuen key text changed to "Done" instead of return
myTextField.clearButtonMode = .whileEditing
myTextField.delegate = self as? UITextFieldDelegate
return myTextField
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(createTextField)
setupUserName()
}
private func setupUserName() {
createTextField.topAnchor.constraint(equalTo: view.topAnchor, constant: (self.view.frame.height * (400/812))) .isActive = true
createTextField.leftAnchor.constraint(equalTo: view.leftAnchor, constant: (self.view.frame.width * (35/375))) .isActive = true
createTextField.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -(self.view.frame.width * (35/375))) .isActive = true
}
答案 0 :(得分:1)
添加以下代码行:
myTextField.translatesAutoresizingMaskIntoConstraints = false
还要增加一些高度:
myTextField.heightAnchor.constraint(equalToConstant: 40).isActive = true