【发布时间】:2020-01-12 23:34:59
【问题描述】:
我试图将我的文本字段的 X 位置居中,使其位于我的 pageviewcontroller 的中心。 这就是我现在的编程方式:
lastnamefield = UITextField(frame: CGRect(x: 100.33, y:431.33 , width: 206, height: 34))
lastnamefield.backgroundColor = .clear
lastnamefield.placeholder = "Last name"
lastnamefield.textColor = .black
lastnamefield.font = UIFont(name: "Futura", size: 20)
lastnamefield.borderStyle = UITextField.BorderStyle.roundedRect
lastnamefield.autocorrectionType = UITextAutocorrectionType.no
lastnamefield.keyboardType = UIKeyboardType.default
lastnamefield.returnKeyType = UIReturnKeyType.done
lastnamefield.clearButtonMode = UITextField.ViewMode.whileEditing
lastnamefield.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
// lastnamefield.delegate = self as! UITextFieldDelegate
self.view.addSubview(lastnamefield)
emailfield = UITextField(frame: CGRect(x: 100.33, y:488 , width: 206, height: 34))
emailfield.backgroundColor = .clear
emailfield.placeholder = "Email Adress"
emailfield.textColor = .black
emailfield.font = UIFont(name: "Futura", size: 20)
emailfield.borderStyle = UITextField.BorderStyle.roundedRect
emailfield.autocorrectionType = UITextAutocorrectionType.no
emailfield.keyboardType = UIKeyboardType.default
emailfield.returnKeyType = UIReturnKeyType.done
emailfield.clearButtonMode = UITextField.ViewMode.whileEditing
emailfield.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
// emailfield.delegate = self as! UITextFieldDelegate
self.view.addSubview(emailfield)
我知道这显然不会居中,但我已经尝试过这样的事情:
emailfield.widthAnchor.constraint(equalTo: revealingSplashView.widthAnchor, multiplier: 0.8).isActive = true
imageView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
emailfield.heightAnchor.constraint(equalToConstant: 25).isActive = true
emailfield.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
我的应用程序要么崩溃,要么出错。我对这个约束有点陌生,我必须以编程方式进行,因为我的 pageviewcontroller 也是以编程方式进行的。 我为我糟糕的写作技巧道歉。我不是英国人。
【问题讨论】:
-
请分享您的崩溃日志和错误消息。
-
要将视图置于其超级视图的中心,您可以简单地尝试
lastnamefield.center = view.center
标签: ios swift xcode constraints