klundberg / KRLCollectionViewGridLayout

A UICollectionViewLayout that specifies item size and location by number of columns.
MIT License
164 stars 23 forks source link

New feature request: Layout a grid inside a fixed frame #5

Open chmaynard opened 8 years ago

chmaynard commented 8 years ago

I would like to use this class to implement a game that always displays 12 cells in a grid with 4 rows and 3 columns. I want the aspect ratio of each cell to be 2x3. The collection view's frame is fixed and its dimensions will depend on the device. (I’m assuming that the base class UICollectionViewLayout can figure out the correct size of the collection view’s frame regardless of the device.)

I don’t think the standard flow layout can do this. In order to accomplish what I want, the class will need to adjust the margins (section insets) and the item spacing (distance between cells) to fit the grid inside the frame and make it look regular. In other words, the item spacing and insets should all be the same distance in points.

I plan to spend some time working on this packing problem. If I can find a solution, I’ll go ahead and submit a pull request. Suggestions welcome!

klundberg commented 8 years ago

Great, thanks! I'm not quite sure I fully understand the issue though. It seems like by your description everything should be able to be handled by the layout as it exists today. Could this not simply work with the code as it is today by assigning the same numbers to sectionInset, interItemSpacing, and lineSpacing, and by assigning the expected aspect ratio/number of items per line? Or am I missing something here?

chmaynard commented 8 years ago

Your class takes all of these constants and creates a grid layout with a content size big enough to contain the grid. The scroll view allows the user see the whole grid.

I want to make the content size a constant, and have the class calculate the item size, insets, and spacing that makes it all work in a fixed frame. No scrolling.

klundberg commented 8 years ago

Aha, I see. That's a good idea. The fixed frame part with no scrolling is interesting to think about. I designed this layout to behave similarly to flow layout such that scrolling is a natural consequence, so adding a no-scrolling mode might be tricky, especially since it would involve constraining the vertical space which would cause the layout to probably have to calculate cell frames very differently. I wonder if making a new layout based on this with those new properties might be better for simplicity's sake (and if that's the case then hosting two layouts in this repo isn't out of the question if they're related), but I'm definitely open to seeing how an implementation here could work within one class.

chmaynard commented 8 years ago

Maybe UICollectionView is not well suited to what I need, but nothing else comes to mind.

klundberg commented 8 years ago

I think collection view could work well here; I was just referencing that my layout wasn't designed to be explicitly non-scrollable, so i worry that your use case may be difficult to accomplish explicitly here. I could be wrong though, and I don't mean to discourage any attempt you wish to make!