Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate constraints without calling `layoutIfNeeded`?

Calling [self.view layoutIfNeeded]; in the animation block causes all of its child views to animate.

This causes a problem whilst scrolling a UICollectionView because the UICollectionViewCells animates onto the screen.

enter image description here

Is there anyway I can stop the Portsmouth cell from animating in?

** EDIT **

To add insult to injury I am animating the constraints that are controlling the height of the UICollectionView. To increase the size of the feed, I have a disappearing UIView acting like a header.

like image 915
William George Avatar asked Aug 07 '14 13:08

William George


2 Answers

You can layoutIfNeeded on any view, so perhaps consider changing the view hierarchy of your cell to isolate the view that contains the constraints that need animating. So that your layoutIfNeeded call only triggers animations on the constraints you intend to animate. Does that make sense?

This would mean inserting a UIView into your hierarchy, and this view would become the superview of the views you are animating via constraints.

like image 165
Alfie Hanssen Avatar answered Sep 20 '22 18:09

Alfie Hanssen


I've spent hours trying to fix this bug, and finally I've found a solution:

[UIView setAnimationsEnabled:NO];
       // Layout cell
[UIView setAnimationsEnabled:YES];

I've been using frames, not auto-layout, but maybe there's a way to disable animations for constraint layouting too.

EDIT:

You could try:

-(void)layoutSubviews
{
[UIView setAnimationsEnabled:NO];
   [super layoutSubviews];
[UIView setAnimationsEnabled:YES];
}
like image 30
michal.ciurus Avatar answered Sep 22 '22 18:09

michal.ciurus