Jordan H
Jordan H

Reputation: 55835

Reset NSLayoutConstraint constant to its original value from the Storyboard

Imagine the scenario where you have laid out a UIView with an NSLayoutConstraint that has different constants for several of the size classes in your Storyboard. Now you want to animate the position of this view by modifying that constraint's constant. Let's say you want to animate it from -100 to its original value that you set in the storyboard for the current size class. How can you get that value to use in the animation code?

That way you wouldn't have to detect the size class in code and set it all programmatically with the values that align with what was already set in the Storyboard.

Upvotes: 3

Views: 1843

Answers (2)

Sumit Oberoi
Sumit Oberoi

Reputation: 3475

1) create an outlet for your constraint

2) in the condition when you want to animate the constraint change constraint.constant value to the value you want to set

3) In your animation block call view.layoutIfNeeded()

Upvotes: 1

i_am_jorf
i_am_jorf

Reputation: 54620

Connect your layout constraint to an outlet in Interface Builder. Then when your view is loaded, cache the value of constraint.constant in an ivar. Then you can change it and reset it however you wish.

Upvotes: 0

Related Questions