Adrian
Adrian

Reputation: 20068

UIImageView doesn't animate when using Auto Layout

My image has the following constraints:

I want my image to start from the right side of the screen and move to initial place where the constraints are set.

Here is my code:

imageLeadingConstraint.constant = 100
imageTrailingConstraint.constant = -100
imageTopConstraint.constant = 100
image.layoutIfNeeded()

UIView.animate(withDuration: 1.0) {
    self.imageLeadingConstraint.constant = 0
    self.imageTrailingConstraint.constant = 0
    self.imageTopConstraint.constant = 75
    self.image.layoutIfNeeded()
}

The issue is that the image is moving to initial place, but not using an animation.
Is my code right ? How come the image just appears in the new position and not animating ?

Upvotes: 1

Views: 118

Answers (2)

Nikita Ermolenko
Nikita Ermolenko

Reputation: 2259

You should call layoutIfNeeded for imageView's superview.

self.image.superview.layoutIfNeeded()

Upvotes: 3

Nikita Patskov
Nikita Patskov

Reputation: 457

I'm not sure that I understood you, but: 1) Change constraints (second time) outside of animation block. 2) use self.image.superview?.layoutIfNeeded() instead

Upvotes: 0

Related Questions