gpt4 book ai didi

ios - 自定义文本字段不符合约束

转载 作者:行者123 更新时间:2023-12-02 14:25:42 25 4
gpt4 key购买 nike

我一直遇到这个问题 HoshiTextField很长一段时间以来,我也在 git 上提出了一个问题,但没有得到答案......

这就是它应该的样子,以及如果用户选择 textField 时的样子。 :

enter image description here

设置textField时出现问题至becomeFirstResponder里面viewDidLoad或当 popping一个ViewControllertextField第一个里面VC被选中。显然这搞乱了 framesconstraints textField的但我完全不知道如何解决这个问题。

enter image description here

正如您所看到的,“Email-Adesse”文本正在移动到左上角,并且当 pushing 时和poping返回ViewController它甚至超出了 constraints 。检查View Hirarchy时使用调试器,“电子邮件地址”文本看起来完美到位,即使事实并非如此。设置 viewDidAppear 内的约束没有改变任何东西。

我限制 textFields像所有其他元素一样:

let emailTextField: HoshiTextField = {
let v = HoshiTextField()
v.borderActiveColor = .white
v.borderInactiveColor = .white
v.textColor = .white
v.font = UIFont(name: "AvenirNext-Regular", size: 17)
v.placeholder = "Email-Adresse"
v.placeholderColor = .white
v.placeholderFontScale = 0.8
v.minimumFontSize = 13
v.borderStyle = .line
v.autocapitalizationType = .none
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
emailTextField.topAnchor.constraint(equalTo: theLabel.bottomAnchor, constant: 20).isActive = true
emailTextField.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 30).isActive = true
emailTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true
emailTextField.heightAnchor.constraint(equalToConstant: 60).isActive = true

如果有人能帮助我,我将不胜感激!希望问题清楚了,你也可以看看我的项目,自己看看问题:

Git repo to my project

最佳答案

这是完整的测试 Controller ,其中包含 2 个可能的解决方案替代方案

演示:备用 1 - 通常显示已展开

demo

演示:Alternate 2 - 总是有延迟,扩展可见

demo2

class ViewController2: UIViewController {
@IBOutlet weak var theLabel: UILabel!
private weak var emailTextField: HoshiTextField!

override func viewDidLoad() {
super.viewDidLoad()

emailTextField = {
let v = HoshiTextField()
v.borderActiveColor = .white
v.borderInactiveColor = .white
v.textColor = .white
v.font = UIFont(name: "AvenirNext-Regular", size: 17)
v.placeholder = "Email-Adresse"
v.placeholderColor = .white
v.placeholderFontScale = 0.8
v.minimumFontSize = 13
v.borderStyle = .line
v.autocapitalizationType = .none
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
self.view.addSubview(emailTextField)
emailTextField.topAnchor.constraint(equalTo: theLabel.bottomAnchor, constant: 20).isActive = true
emailTextField.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 30).isActive = true
emailTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true
emailTextField.heightAnchor.constraint(equalToConstant: 60).isActive = true

// DispatchQueue.main.async {
// self.emailTextField.becomeFirstResponder() // Alternate 1
// }
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
emailTextField.becomeFirstResponder() // Alternate 2
}
}

关于ios - 自定义文本字段不符合约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60670915/

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