Jump to content
  • entries
    334
  • comments
    900
  • views
    258,300

iOS programming - battling with UICollectionView


EricBall

738 views

In my game I want a level builder. Users will be able to create their own levels and send them to me for inclusion in a future release. The UI is fairly simple - a level grid in the top of the screen and the level tiles in the bottom of the screen. Select a tile then where you want it in the grid. The tiles are in a scrolling view like a photo gallery. The iOS UIKit SDk even provides an out-of-the box solution - the UICollectionView (+UICollectionViewFlowLayout). It's suppose to be easy; not quite drag & drop, but fairly close.

The problem is the size of the tiles. An iPhone 4s is 640x960 pixels (320x480 "points") while an iPad is 768x1024 or 1536x2048 pixels (both 768x1024 "points"). So if I make the tiles 64x64 "points" the tiles end up much smaller relative to the level grid (which is resized to the screen size). There's no option in Interface Builder to make the size of the cell (which stores the tile) relative to the screen size.

And 'cause I'm just learning, I don't 100% understand what I'm doing. So it's a lot of trial and error. Google searches have given me some info, and I think I can set the cell size for the layout, but I haven't found anything which says how to properly resize the cell contents (and my attempts so far have failed).

I'm thinking there are two probable causes - first is execution order. The collection view does some buffering and preloading and I think that may be occurring before I'm calculating the size of the cells. Second is Interface Builder creates some implicit code in my application which might be blocking my efforts. My current focus is on the execution order to see how early I can determine the size of the cells so I can use that value elsewhere in the code.

Or maybe I need to find some sample code for fully dynamically sized collection views and reuse that.

1 Comment


Recommended Comments

I managed to get this working (I think). I started to look at some sample code for dynamically sized cells, but ran into more knowledge issues. (Although the code did have some interesting constructs.) But then I found some sample code which used constraints inside the cell and I had a light bulb moment. If I included layout constraints between the cell and the imageview inside the cell, then the imageview would be automatically resized when I resized the cell. Bingo!

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...