0

I am updating a UIView with layoutIfNeeded within an animateWithDuration block, but the duration is taking much longer than what I have coded. Here is the code:

func showInfoView() {
    self.hintConstraint.constant = self.view.frame.height - (self.infoView.frame.height + 260)
    self.view.layoutIfNeeded()

    infoView.hidden = false
    UIView.animateWithDuration(0.2, animations: {
        self.view.layoutIfNeeded()
    })
}

The animation is taking longer than 0.2 seconds. Even if I set the duration time to 0, which should make the code layout the view without animating, the animation still occurs. Does layoutIfNeeded automatically have a duration when it's within the animateWithDuration block? Or am I missing something?

Mike Walker
  • 2,944
  • 8
  • 30
  • 62
  • Is there a reason why you change a constraint, **layout the view**, change a view property, then layout the view again (in the animation block)? You should be able to change the constraint, call `setNeedsUpdateConstraints,` change the view property, then layout the view in the animation, saving you one (no-op) layout iteration. –  Aug 31 '15 at 17:37
  • I read somewhere, but I can't find where right now, that I should perform layoutIfNeeded before the animate with duration just in case anythings needs to be updated. If I remove that statement, my problem still occurs. – Mike Walker Aug 31 '15 at 17:45
  • [Found this](http://stackoverflow.com/a/12664093/4473451), it looks like you layoutIfNeeded before you change the constraint. Give that a try. – Nightly Sep 01 '15 at 00:13
  • 1
    For some reason, that did the trick. Could you post an answer so I could accept? – Mike Walker Sep 01 '15 at 15:53

0 Answers0