当用户通过设置Right布局约束的动画来开始键入消息时,我正在尝试扩展我的消息文本字段。文本字段确实会扩展,但是它会从中心向两个方向扩展,这并不是理想的效果,因为我仅更新了一个约束,为什么另一个约束会改变?
func animateTextField(textfield : UITextField, constraint : NSLayoutConstraint) {
let isTextFieldExpanded = constraint.constant == 15
constraint.constant = isTextFieldExpanded ? 59 : 15
UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseInOut, animations: {
textfield.layoutIfNeeded()
textfield.translatesAutoresizingMaskIntoConstraints = false
})
}
有关更多上下文,我在同一View模型中调用该方法,然后从我的View Controller中调用该方法。
func handleTypingAnimation(view : UIView, textField : UITextField, constraint : NSLayoutConstraint) {
if textField.text?.isEmpty == false {
animateSendButtonAppear(view: view)
animateTextField(view: view, textfield: textField, constraint: constraint)
}else{
animateSendButtonHide(view: view)
}
}
我的视图控制器
@IBAction func didStartTyping(_ sender: UITextField) {
messageVM.handleTypingAnimation(view: sendButtonView, textField: messageTextField, constraint: textfieldRightConstraint)
}
我的布局约束
@IBOutlet var textfieldRightConstraint: NSLayoutConstraint!
答案 0 :(得分:0)
您可以尝试
UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseInOut, animations: {
self.view.layoutIfNeeded()
// textfield.translatesAutoresizingMaskIntoConstraints = false
}
在更改孩子的约束条件时,应该始终重新布置父级