roberthein / TinyConstraints

Nothing but sugar.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

constraint changes fail to animate -- SOLVED

eliburke opened this issue · comments

I've struggled for several hours now trying to figure out why my constraint changes weren't animating.

There is no issue with TinyConstraints (which is great, thanks Robert) just user error. But I thought it was worth a footnote in the README or an archived issue to hopefully save someone else the same hair pulling.

If you are following the suggested method of animating a set of constraints, and they immediately take effect without any lovely animation, make sure you are calling setNeedsLayout() on the superview containing the views you want to animate

    constraints1 = subview.edges(to: superview)
     constraints2 = subview.edges(to: superview, insets: UIEdgeInsets(top: 10, left: 10, bottom: 0, right: 0))
    ...
     constraints1.deActivate()
     constraints2.activate()
     UIViewPropertyAnimator(duration: 1, dampingRatio: 0.4) {
          //self.subview.layoutIfNeeded()   // WRONG-- won't animate
          self.view.layoutIfNeeded()        // RIGHT-- will animate
        }.startAnimation()