出现键盘时约束不更新
Constraints Not Updating When Keyboard Appears
我正在制作一个注册屏幕,我想更新一些顶部锚点,这样当键盘出现时,顶部锚点常量会减少并且键盘不会覆盖任何文本字段。
我创建了一个 topConstant 变量:
var constraintConstant: CGFloat = 35
并建立了我的观点如下:
view.addSubview(passwordTextField)
passwordTextField.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 25).isActive = true
passwordTextField.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -25).isActive = true
passwordTextField.heightAnchor.constraint(equalToConstant: 50).isActive = true
passwordTextField.topAnchor.constraint(equalTo: emailTextField.bottomAnchor, constant: constraintConstant).isActive = true
然后我写了这段代码:
func textFieldDidBeginEditing(_ textField: UITextField) {
constraintConstant = 15
view.layoutIfNeeded()
}
我不确定为什么 constriants 没有更新。有什么想法吗?
你需要
var topCon:NSLayoutConstraint!
//
topCon = passwordTextField.topAnchor.constraint(equalTo: emailTextField.bottomAnchor, constant: constraintConstant)
topCon.isActive = true
//
func textFieldDidBeginEditing(_ textField: UITextField) {
topCon.constant = 15
view.layoutIfNeeded()
}
好的。当我第一次使用 Swift 开始 iOS 时,我遇到了这个问题。看到问题出在您对锚点的理解上。
您指定的常量与您期望的不同。 (你期望它像某种监听器一样工作,它会根据变量值的更新不断更新。它不会)它只会在设置时获取变量的值,然后不看返回,除非您访问该锚点并手动更改常量。
这就是为什么您必须像这样存储锚实例并手动更改常量的原因。
定义约束变量:
var topAnchorConstraint: NSLayoutConstraint!
在变量中存储适当的约束条件
topAnchorConstraint = passwordTextField.topAnchor.constraint(equalTo: emailTextField.bottomAnchor, constant: 35)
topAnchorConstraint.isActive = true
现在您需要根据需要更改常量。
func textFieldDidBeginEditing(_ textField: UITextField) {
UIView.animate(withDuration: 1.0, animations: {
self.topAnchorConstraint.constant = 15
self.view.layoutIfNeeded()
}, completion: nil)
}
我正在制作一个注册屏幕,我想更新一些顶部锚点,这样当键盘出现时,顶部锚点常量会减少并且键盘不会覆盖任何文本字段。
我创建了一个 topConstant 变量:
var constraintConstant: CGFloat = 35
并建立了我的观点如下:
view.addSubview(passwordTextField)
passwordTextField.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 25).isActive = true
passwordTextField.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -25).isActive = true
passwordTextField.heightAnchor.constraint(equalToConstant: 50).isActive = true
passwordTextField.topAnchor.constraint(equalTo: emailTextField.bottomAnchor, constant: constraintConstant).isActive = true
然后我写了这段代码:
func textFieldDidBeginEditing(_ textField: UITextField) {
constraintConstant = 15
view.layoutIfNeeded()
}
我不确定为什么 constriants 没有更新。有什么想法吗?
你需要
var topCon:NSLayoutConstraint!
//
topCon = passwordTextField.topAnchor.constraint(equalTo: emailTextField.bottomAnchor, constant: constraintConstant)
topCon.isActive = true
//
func textFieldDidBeginEditing(_ textField: UITextField) {
topCon.constant = 15
view.layoutIfNeeded()
}
好的。当我第一次使用 Swift 开始 iOS 时,我遇到了这个问题。看到问题出在您对锚点的理解上。
您指定的常量与您期望的不同。 (你期望它像某种监听器一样工作,它会根据变量值的更新不断更新。它不会)它只会在设置时获取变量的值,然后不看返回,除非您访问该锚点并手动更改常量。
这就是为什么您必须像这样存储锚实例并手动更改常量的原因。
定义约束变量:
var topAnchorConstraint: NSLayoutConstraint!
在变量中存储适当的约束条件
topAnchorConstraint = passwordTextField.topAnchor.constraint(equalTo: emailTextField.bottomAnchor, constant: 35)
topAnchorConstraint.isActive = true
现在您需要根据需要更改常量。
func textFieldDidBeginEditing(_ textField: UITextField) {
UIView.animate(withDuration: 1.0, animations: {
self.topAnchorConstraint.constant = 15
self.view.layoutIfNeeded()
}, completion: nil)
}