-1

After seeing the wonderful Advances in Collection View Layout Session, I wondered if is possible to use the capabilities implemented in the new Collection View Compositional Layouts framework to create a layout based on previously unknown cell sizes.

My problem:

Imagine a horizontal image gallery with two levels, the photos can be "vertical" or "horizontal", and the app is fetching them when are needed for display, and they are provided randomly ("horizontally" or "vertically"). So we have to populate the gallery with a "random pattern" (Pinterest style, here is how to do it with UICollectionViewFlowLayout).

Random layout illustrating the question

I cannot see a "group" pattern to populate the cells, because the layout has infinite possibilities, vertical divisions can appear to create groups, but also can not.

Is it possible to archive with Collection View Compositional Layouts?


Thank you.

Jorge Frias
  • 1,077
  • 11
  • 13
  • Did you ever get anywhere with this? I'm trying to do something similar in the vertical scrolling direction but not seeing a way of doing it. – Benjamin Hall Mar 11 '20 at 13:08
  • @BenjaminHall With ``Collection View Compositional Layouts`` I couldn't do it. In the question you have a link to a tutorial on how to do it for a vertical layout with ``UICollectionViewFlowLayout``. – Jorge Frias Mar 15 '20 at 16:40

1 Answers1

0

When you're setting your item layout size, you can use an estimated layout dimension to make the size dependent on its contents. So an image view can set the cell size from the inside out using constraints. You size the image when you populate the cell, depending on whether it is horizontal or vertical, and the image view adopts the size of the image and the cell adopts the size of the image view.

(If you needed even more control you could construct your group as a NSCollectionLayoutGroup.custom, which allows you set every item's frame manually. But there's no need for such extremes in this simple situation, and besides, that assumes you know the frame at layout declaration time, which you probably don't.)

matt
  • 515,959
  • 87
  • 875
  • 1,141