Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView with only 1 vertical 'column' still flowing into 2 columns

Tags:

ios

iphone

I've created what I thought was a basic UICollectionView using xCode 5, iOS 7, Storyboards, AutoLayout OFF, however I'm getting strange results.

I had hoped to create a single "column" - that is, all cells stacked vertically, none side-by-side (like a TableView for example):

| 1 |

| 2 |

| 3 |

| 4 |

| 5 |

However, I'm getting 2 columns like this (which I can only see if I scroll horizontally):

| 1 | | 2 |

| 3 | | 4 |

| 5 |

My CollectionView is the same width as my cells, 1 section, no insets, no headers, scroll direction=vertical, layout=Flow.

EDIT: I tried adding 5 cells via the Storyboard and they lined up exactly as I want, but still do not line up when added programatically...

EDIT2: FYI, I also another UICollectionView which works fine - the only difference is that it is 4x4

How can I achieve the 'single column' layout?

like image 719
wayneh Avatar asked Oct 31 '22 19:10

wayneh


1 Answers

Look into implementing -(CGSize)collectionView:layout:sizeForItemAtIndexPath: of the UICollectionViewDelegateFlowLayout. You can adjust your cells based on the collection view bounds. The flow layout is currently taking advantage of the fact that 2 of your cells can fit side-by-side while the device is in landscape orientation.

Another approach is to modify the inset for a section (ie, you can pad such that a single column is all you see) using -(UIEdgeInsets)collectionView:layout:insetForSectionAtIndex:

like image 107
azsromej Avatar answered Nov 15 '22 05:11

azsromej