2

I'm trying to switch layouts in a ASCollectionNode. When using standard UICollectionView you can call

self.collectionView.setCollectionViewLayout(layout, animated: true)

to replace/update existing layout.

This, however, doesn't work with ASCollectionNode. Calling

self.collectionView.view.setCollectionViewLayout(layout, animated: true)

does nothing. How would one achieve such functionality?

Pavlo Muratov
  • 317
  • 1
  • 14
  • If you want to change layouts for cells, ASCollectionNode uses cell self-calculation. – Bimawa Apr 13 '18 at 08:56
  • @Bimawa that means I just get rid of the several layouts, leave 1 basic flow layout and do all the work in the cells.. – Pavlo Muratov Apr 13 '18 at 08:59
  • yes, for clear understand how it works try use breakpoint in constrainedSize inside the cell, and you see your container. if you fill not full that CollectionNode will try to insert one more cell. – Bimawa Apr 13 '18 at 09:02
  • @Bimawa I removed layouts and left only one. Now I return different layout specs from cells and size ranges from constrainedSizeForNodeAt method. Cells started to change their size as expected, but collection is not updating. I've tried calling invalidateCalculatedLayout, transitionLayout methods on collection node - no luck :( Any suggestions? – Pavlo Muratov Apr 13 '18 at 13:12
  • What do you need for a call with relayout? just in my case its fully depends on CellNodes. – Bimawa Apr 15 '18 at 00:38
  • @Bimawa you mean you do not even implement constrainedSizeForNodeAt method? – Pavlo Muratov Apr 16 '18 at 11:55
  • Yes, you no need that callback with Texture, height is CGFLOAT_MAX by default in 'constrainedSize' cell. You can skype to me on bimawa login. – Bimawa Apr 16 '18 at 15:31
  • Check your skype – Pavlo Muratov Apr 16 '18 at 15:38

1 Answers1

1

So, what one needs to do to achieve the described functionality, is to get rid of all UICollectionViewLayout subclasses and leave only 1 instance of base UICollectionViewLayout. Then, override layoutSpecThatFits method inside ASCellNode subclass and return a layout specs based of some flag, enumeration or any other mechanism one sees fit to choose between collection display modes. This is essentially a replacement for the several layout mechanism used for default collection views. Then, implement a ASCollectionViewLayoutInspecting protocol with collectionView:constrainedSizeForNodeAt: method (in view controller, most likely), return ASSizeRange's appropriate for each display mode. At last, call relayoutItems method on your ASCollectionNode instance to kickoff the layout recalculation. Wrap it in UIView animation block, if needed.

P.S. I think it's worth mentioning that if in collectionView:constrainedSizeForNodeAt: you return the same range value for each display mode (maybe you only want to change layout spec inside the cells, but not their actual size), then relayoutItems will not do anything, basically. ASDK will use the cached layout, because it will consider it valid based on the fact that size range value didn't change. You may need to come up with some layout invalidation strategy to make relayoutItems work is such scenario.

Pavlo Muratov
  • 317
  • 1
  • 14