Search code examples
iosios6uiviewanimationautolayout

Animated constraint change layoutIfNeeded slow


I'm trying to animate a my ADBannerView on and off screen using autolayout. Pre-autolayout (setting frame and animating) everything worked fine. Now I have the following code and it runs on viewDidLoad to make sure the ad banner if off the screen.

[UIView animateWithDuration:kADAnimationDuration
                 animations:^{
                     _addBannerDistanceFromBottomConstraint.constant = 32;
                     [self.view layoutIfNeeded];    
}];

This code work, but it is slow. layoutIfNeeded is taking a long time (relatively) and causes poor performance. Removing the line makes everything nice and fast, but the animation doesn't work.

The question is... why is this so slow/how do I speed it up?

Edit: My UI is fairly significant and I thought laying out everything might be the cause, so I tried creating a simple UIView and putting my banner in there as a subview, then I only needed to call layoutIfNeeded on that subview with 1 item in it. No change in performance though.


Solution

  • I created an app with the ADBannerView and nothing else. Same problem existing. I submitted to Apple as a bug and got "duplicate" so I guess it's a known issue.

    As a workaround I'm starting with the ad banner off the screen and moving on the screen only when I get a valid ad. While it's slightly more annoying to work with in the storyboard, the user won't notice the difference and it prevents the slow down.

    Edit: I've been working more with auto layout and I've come to a unfortunate conclusion which I seem to share with Matt Neuburg. In bit he wrote titled "auto layout slow on iOS" he said:

    It's a pity, but auto layout is slow on iOS.

    It pretty much has summed up several issues I've been having converting one of my apps to auto layout. The performance is simply not there. The example Matt gives in his post has auto-layout taking 5 times longer to accomplish the same task.

    For now, I guess I'll be avoiding auto layout for anything complicated enough for auto layout to be useful.

    Edit: iOS 8 is in beta and constraints have gotten significantly faster. There is still when using a lot (50+) especially on older devices but from testing so far the formerly 2 second delay I was getting has been reduced to about 0.4 seconds. With this change constraints may have been sped up enough to be viable for some constraint heavy view work.