Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With the help of Chase Roberts, I now found the solution to my problem. My code basically looks like this:</p> <pre><code>// Prepare for animation [self.collectionView.collectionViewLayout invalidateLayout]; UICollectionViewCell *__weak cell = [self.collectionView cellForItemAtIndexPath:indexPath]; // Avoid retain cycles void (^animateChangeWidth)() = ^() { CGRect frame = cell.frame; frame.size = cell.intrinsicContentSize; cell.frame = frame; }; // Animate [UIView transitionWithView:cellToChangeSize duration:0.1f options: UIViewAnimationOptionCurveLinear animations:animateChangeWidth completion:nil]; </code></pre> <p>For further explanation:</p> <ol> <li><p>One of the most important step is the <code>invalidateLayout</code> call on the <code>UICollectionView.collectionViewLayout</code> you are using. If you forget it, it is likely to happen that cells will grow over the borders of your collection - that's something you most probably don't want to happen. Also consider that your code should at some point present the new size of your cell to your layout. In my case (I'm using an <code>UICollectionViewFlowLayout</code>), I adjusted the <code>collectionView:layout:sizeForItemAtIndexPath</code> method to reflect the new size of the modified cell. Forgetting that part, nothing will happen to your cell sizes at all if you first call <code>invalidateLayout</code> and then try to animate the change in size.</p></li> <li><p>Most strange (at least to me) but important is that you call <code>invalidateLayout</code> <em>not</em> inside the animation block. If you do so, the animation won't display smooth but glitchy and flickering.</p></li> <li><p>You have to call <code>UIView</code>s <code>transitionWithView:duration:options:animations:completion:</code> method with the cell as argument for <code>transitionWithView</code>, not the whole collection view, because it's the cell you want to get animated, not the entire collection.</p></li> <li><p>I used the <code>intrinsicContentSize</code> method of my cell to return the size I want - in my case, I grow and shrink the width of the cell to either display a button or hide it, depending on the selection state of the cell.</p></li> </ol> <p>I hope this will help some people. For me it took several hours to figure out how to make that animation work correctly, so I try to free others from that time-consuming burden. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload