Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent unwanted animations on subviews when animating constraints

Usually you get unwanted animations on UITableViewCells when animating the height of the UITableView. The new cells that get reused come in and have weird unwanted animations on the subviews that get laid out. This is usually visible when the table expands.

The animations usually look like the objects pop in from a corner since it animates the width and height of the subviews for some reason from 0 to the size they're supposed to be.

This can sort of be solved by overriding in the cell's layoutSubviews method.

- (void)layoutSubviews
{
    [UIView performWithoutAnimation:^{
        [super layoutSubviews];
    }];
}

But this still doesn't prevent the contained subviews from doing unwanted animations. It also doesn't prevent unwanted animations when you add things to contentView of a cell. You basically have to override the layoutSubviews methods of every single subview you have if it too has subviews.

This isn't always possible, like in the case of contentView. I can't override the contentView class and tell it to lay out its subviews without animations. This is also a very cumbersome fix since I need to add this to every single UIView object that I am able to override.

I still haven't found the answer that fixes this once and for all. How to just animate a constraint on something but prevent strange unwanted animations on its subviews.

like image 865
iseletsky Avatar asked Mar 09 '15 22:03

iseletsky


1 Answers

I encountered a similar problem in a UICollectionView and was able to fix it in its delegate:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // setup your cell
    // ...

    [UIView performWithoutAnimation:^{
        [cell layoutIfNeeded];
    }];

    return cell;
}
like image 76
Mickaël Avatar answered Sep 23 '22 12:09

Mickaël