Note that there are some explanatory texts on larger screens.

plurals
  1. POUICollectionViewLayout doesn't update zIndex when changing layouts
    primarykey
    data
    text
    <p>I have two UICollectionViewLayouts that work fine when used on their own but when switching between the two layouts using setCollectionViewLayout:animated: the zIndex of the layout being animated to doesn't use its own zIndex values, it instead uses the zIndex values of the original layout.</p> <p>Here are what the two layouts look like on their own:</p> <p>In layout 1 the cells are stacked with item 0 on top.</p> <p><img src="https://i.stack.imgur.com/olNnl.png" alt="enter image description here"></p> <p>In layout 2 the cells are stacked with item 0 on the bottom.</p> <p><img src="https://i.stack.imgur.com/g4UPl.png" alt="enter image description here"></p> <p>In layout 1 I set the zIndex with:</p> <pre><code>[attributes setZIndex:100 - [indexPath item]]; </code></pre> <p>In layout 2 I set the zIndex with:</p> <pre><code>[attributes setZIndex:100 + [indexPath item]]; </code></pre> <p>If I start with layout 1 and change to layout 2 I end up with this:</p> <p><img src="https://i.stack.imgur.com/igeSj.png" alt="enter image description here"></p> <p>If I start with layout 2 and change to layout 1 I end up with this:</p> <p><img src="https://i.stack.imgur.com/B6don.png" alt="enter image description here"></p> <p>I've experimented with setting the zIndex in both layoutAttributesForElementsInRect: and layoutAttributesForItemAtIndexPath: and what I've found is that when switching layouts, layoutAttributesForElelmentsInRect: gets called once on the layout that is being animated to and layoutAttributesForItemAtIndexPath: gets called many times on both layouts as the animated transition is happening.</p> <p>I also noticed that the calls to layoutAttributesForItemAtIndexPath: were being called on the layout being animated from after being called on the layout being animated to. This lead me to try something odd, which was to set the correct zIndex for a given layout in layoutAttributesForElementsInRect: but to set the zIndex for the other layout in layoutAttributesForItemAtIndexPath:. This actually results in the correct layout after the animation is completed in either direction but the animation from layout 2 to layout 1 looks bad because the zIndex doesn't update till the very end of the animation.</p> <p>Here is what it looks like right before the animation from layout 2 to layout 1 completes (and corrects the zIndex):</p> <p><img src="https://i.stack.imgur.com/jNgYP.png" alt="enter image description here"></p> <p>To deal with seeing this in between state I am considering making an intermediate layout that only changes the zIndex of the cells in layout 2 before it transitions to layout 1. Not elegant but I think it may work if I use setCollectionViewLayout:animated:completion: where I call setCollectionViewLayout:animated: inside the completion block of the first layout change.</p> <p>Anyway, it seems crazy to me that I should have to set the zIndex for the opposite layout in layoutAttributesForItemAtIndexPath: in order to get the correct end result of changing layouts. Does anyone have any suggestions for how to implement this in a way that doesn't require this hack?</p> <p><strong>EDIT</strong> I implemented an intermediate layout when switching from layout 2 to layout 1. The intermediate layout is only different in regards to the zIndex being flipped. Interesting to note that to switch between layout 2 and this intermediate layout I did not need to apply the hack in layoutAttributesForItemAtIndexPath: however I did need to call reloadData on the collectionView in the completion block of setCollectionViewLayout:animated:completion for it to work.</p> <p>Would still love to know why the zIndex hack is necessary to correctly switch the other layouts.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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