Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing UICollectionViewCell animated appearance when presenting UICollectionView

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.

like image 499
matehat Avatar asked Jun 24 '13 19:06

matehat


1 Answers

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:

  • Ensure the constraint you're editing is related to the position of the collection view, not the size. By this I mean that you're not presenting your collection view by changing its height from zero to the final value.
  • Call 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.
like image 126
jrturton Avatar answered Nov 05 '22 18:11

jrturton