When a user does some action, I need to pull a UICollectionView
from the bottom up to a certain height. Since that new state is totally optional, the collection view is created just before being presented that way. The animation from bottom to top is performed using changes to NSLayoutConstraint's constant
property and a call to [view layoutIfNeeded]
in some animation block.
The problem is that, doing things that way makes the cells appear in an undesired way: they expand from their top-left corner to their specified size. I would like the collection view to appear and have all of its cells already laid out in their final size and appearance.
I'm aware of things like UIView's setAnimationEnabled:
method, but I can't seem to find how and where I should use that (if that's the way to go).
I guess the problem is due to the collection view cells being added to the view hierarchy just before the animation block that contains the call to [superview layoutIfNeeded]
. This probably leads UIKit to think that it should also animate those changes to the layout. If that's the case the solution would probably be something along the way of excluding from the animation, specific changes to the view hierarchy.
Expanding from the top left corner is typically a symptom of calling layoutIfNeeded
in an animation block when the original view has never been laid out. You're basically animating the initial layout pass, where all subviews have started at CGRectZero.
To solve it you need two things:
layoutIfNeeded
on your view before you edit the constraint, then call it again in the animation block after you've made the change. This way you're only animating the change you've specified to the constraint, rather than the entire layout. If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With