试图快速地制约一个约束

我有一个UITextField,我想扩大它的宽度,当点击。 我设置了约束条件,并确保左边的约束条件具有较低的优先级,然后我试图在右边进行animation处理。

这是我正在尝试使用的代码。

// move the input box UIView.animateWithDuration(10.5, animations: { self.nameInputConstraint.constant = 8 }, completion: { (value: Bool) in println(">>> move const") }) 

这个工作,但似乎只是瞬间发生,似乎没有任何运动。 我试图把它设置为10秒,以确保我没有失去任何东西,但我得到了相同的结果。

nameInputConstraint是我控制从IB连接到我的类的约束的名称。

感谢您的帮助提前!

您需要先更改约束,然后为更新添加animation。

 self.nameInputConstraint.constant = 8 UIView.animateWithDuration(0.5) { self.view.layoutIfNeeded() } 

而在Swift 3.0中:

 UIView.animate(withDuration: 0.5) { self.view.layoutIfNeeded() } 

就我而言,我只更新了自定义视图。

 // DO NOT LIKE THIS customView.layoutIfNeeded() // Change to view.layoutIfNeeded() UIView.animate(withDuration: 0.5) { customViewConstraint.constant = 100.0 customView.layoutIfNeeded() // Change to view.layoutIfNeeded() }