gpt4 book ai didi

ios - Xcode Swift 以编程方式将 UITextfield X 位置设置为中心

转载 作者:行者123 更新时间:2023-11-29 05:10:44 25 4
gpt4 key购买 nike

我尝试将文本字段的 X 位置置于页面 View Controller 的中心。这就是我现在的编程方式:

  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 也是以编程方式。我为我糟糕的写作技巧道歉。我不是英国人。

最佳答案

1-这会崩溃

emailfield.delegate = self as! UITextFieldDelegate

相反

emailfield.delegate = self

并使 vc 符合

class VC:UIViewController,UITextFieldDelegate {

2-

添加约束时不要忘记设置

self.emailfield.translatesAutoresizingMaskIntoConstraints = false

3-对于您设置的电子邮件,宽度,高度和centerX,您还需要设置y约束centerY,顶部或底部

关于ios - Xcode Swift 以编程方式将 UITextfield X 位置设置为中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59709050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com