I am using a UICollectionView programmatically.
I've set its frame as follows:
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 3000) collectionViewLayout:flowLayout];
My UICollectionViewFlowLayout
has a section inset set as:
flowLayout.sectionInset = UIEdgeInsetsMake(20.0, 20.0, 100.0, 20.0);
I also only have 1 section.
I've set the height to be 3,000 to see if the collection view would scroll but it doesn't. I am setting it to that since the collection view doesn't seem to scroll up or down when I insert new items.
UICollectionView
is subclassed from a UIScrollView
, which means I can keep resetting the scroll view content size. Would this work properly?
The only issue is that this would work if I know the sizes of all items including how many items are on each row.
If each item had a different size, how would I find out the size of each row? I would need that to add all row sizes and increase the content size height of self.collectionView
.
EDIT
Even my suggestion above, resetting the content size, does not work properly! I tried the following when updating my collection view:
int numberOfRows = self.dataArray.count / 3;
self.collectionView.contentSize = CGSizeMake(self.view.frame.size.width, (numberOfRows * 200) + 300);
This is very ugly though. This is because it assumes all my images are of size 200x200px, which fits 3 images per row (hence the division by 3).
Furthermore, even with the +300 that I have, the last row I can only see about 3/4 of it and not all of it. I have to scroll more and I will be able to see it but it goes back up again to 3/4. Its kind of like the scroll to refresh, where the view only moves if you drag it and when you leave it bumps back to where it was. Why is this happening? Is it just that Apple designed UICollectionView
so poorly? This is a little ridiculous... UITableViews adjust their scroll according to their content automatically.