我一直遇到这个问题 HoshiTextField很长一段时间以来,我也在 git 上提出了一个问题,但没有得到答案......
这就是它应该的样子,以及如果用户选择 textField
时的样子。 :
设置textField
时出现问题至becomeFirstResponder
里面viewDidLoad
或当 popping
一个ViewController
而textField
第一个里面VC
被选中。显然这搞乱了 frames
或constraints
textField
的但我完全不知道如何解决这个问题。
正如您所看到的,“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
如果有人能帮助我,我将不胜感激!希望问题清楚了,你也可以看看我的项目,自己看看问题:
最佳答案
这是完整的测试 Controller ,其中包含 2 个可能的解决方案替代方案
演示:备用 1 - 通常显示已展开
演示:Alternate 2 - 总是有延迟,扩展可见
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/