1

Since iOS 10 I have noticed animating a layout change ( layoutIfNeeded() ) isn't animating. Here is my UIView extension that works great on iOS 9 and below.

func slideIn(from edgeConstraint: NSLayoutConstraint, withDuration duration: Double = 0.25, finishedAnimating: (() -> Void)? = nil) {
    dispatch_async(dispatch_get_main_queue()) {
        edgeConstraint.constant = 0
        UIView.animateWithDuration(duration,
            delay: 0.0,
            options: .BeginFromCurrentState,
            animations: { self.layoutIfNeeded() },
            completion: { didComplete in
                finishedAnimating?()
        })
    }
}

func slideOut(from edgeConstraint: NSLayoutConstraint, withDuration duration: Double = 0.25, finishedAnimating: (() -> Void)? = nil) {
    dispatch_async(dispatch_get_main_queue()) {
        edgeConstraint.constant = -self.frame.height
        UIView.animateWithDuration(duration,
            delay: 0.0,
            options: .BeginFromCurrentState,
            animations: { self.layoutIfNeeded() },
            completion: { didComplete in
                finishedAnimating?()
        })
    }
}

Does anyone know why it isn't animating?

SeanRobinson159
  • 894
  • 10
  • 19

1 Answers1

11

In your animation blocks you're calling self.layoutIfNeeded() where self is the instance of UIView that you want to animate. Calling layoutIfNeeded() redraws the view that the method is called upon and all if it's subviews. In your case, you don't want to redraw the UIView, you want to redraw the view's superview.

Your functions would make sense and work properly if they were called in a view controller but since they are called in an extension on the UIView itself, you need to call something like view.superview?.layoutIfNeeded()

Aaron
  • 6,466
  • 7
  • 37
  • 75
  • So that worked. But why did it work on iOS 9 without referring to the superview, and now on iOS 10 I have to specify the superview? – SeanRobinson159 Sep 16 '16 at 22:19
  • The bug should be OS agnostic so I'm not sure exactly why it would be working on iOS 9. – Aaron Sep 16 '16 at 23:55
  • It is not actually OS agnostic. This used to really work on 9 and older versions. This new behaviour which turns out to be a fixed on a bug before was introduced on iOS10 - http://stackoverflow.com/a/39548367/1045672 - See Apple release note. – Teffi Sep 27 '16 at 13:55