icekomo icekomo - 1 month ago 12
Swift Question

trying to animate a constraint in swift

I have an UITextField that I want to enlarge it's width when tapped on. I set up the constraints and made sure the constraint on the left has the lower priority then the one that I am trying to animate on the right side.

Here is the code that I am trying to use.

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


This works, but it seems to just happen instantly and there doesn't seem to be any movement. I tried to set it 10 seconds to make sure I wasn't missing anything, but I got the same results.

nameInputConstraint is the name of the constraint that I control dragged to connect into my class from IB.

Thanks for your help in advance!

Answer

You need to first change the constraint and then animate the update.

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