1

My image has the following constraints:

  • leading = 0
  • trailing = 0
  • top = 75
  • height = 250

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 ?

2 Answers 2

3

You should call layoutIfNeeded for imageView's superview.

self.image.superview.layoutIfNeeded()
Sign up to request clarification or add additional context in comments.

1 Comment

The user has tagged this question as swift based, your answer was in objective-C.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.