Reputation: 355
A textfield comes up when I uncomment the CGRect but will not come up if I comment it out. If I use the CGrect, my constraints will not work.
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
}
Upvotes: 1
Views: 52
Reputation: 1125
add this line of code:
myTextField.translatesAutoresizingMaskIntoConstraints = false
also add some height:
myTextField.heightAnchor.constraint(equalToConstant: 40).isActive = true
Upvotes: 1