Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView Decoration and Supplementary views can not be moved

In UICollectionView decoration and supplementary views seem to be a big mystery. There seems to be next to no example code at the moment. I managed to get both types working in a custom layout (see this post for some details). As long as they remain in the same position everything is fine (i.e. if their layoutAttributes.frame does not change).

However as soon as I re-layout with changed layoutAttributes for either decoration or supplementary views they get visually duplicated - i.e. there is a copy in the background at their original location and a copy at their new location. The behaviour is identical if I instantiate them from XIBs or completely in code and the behaviour does not occur for normal cells.

At first I thought that it is some kind of redraw problem, but these "copies" survive re-layouting, redrawing etc. They are not real copies however, since the layoutAttributesForDecorationView etc. are never called for them (only for the new locations). There seems to be some caching in the background in the UICollectionView.

Has anybody got this working or have any ideas. I must say that I am new to the iOS platform, so it could also be simple things like setting the "Clips Bound" or "Clear Graphics Context" properties (I tried those, but it could be something similar).

This is driving me crazy and it is strange that there is absolutely no example code out there.

I am asking myself: Are the decoration and supplementary views not meant to be repositioned? (I hope not)

like image 689
dominikk Avatar asked Oct 12 '12 10:10

dominikk


1 Answers

Hi this is an old question but there is an answer for it.

Actually I had these issues too when I implemented my own Layout object, until I realised it is imperative to cache any layout attributes objects you create in your custom layout. There is actually a very vague reference to this in the documentation (I can't remember the precise location now) which is very easily misinterpreted.

Essentially once you have requested a layoutAttributes object for a cell at an index path, you should retain the attributes object (e.g. retaining it in a dictionary with the index path as a key will do) and return the same attributes object for any and all subsequent requests for the attributes. If you don't do this, but instead recreate new layout attributes, animation of batch updates results in severe graphical glitches and artefacts.

like image 174
TheBasicMind Avatar answered Oct 18 '22 11:10

TheBasicMind