Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView working on iOS7 but not on iOS6

My UICollectionView cells don't get displayed on iOS6, because my delegate method cellForItemAtIndexPath doesn't get called. I suspect because of this warning:

 the behavior of the UICollectionViewFlowLayout is not defined because:
 the item height must be less that the height of the `UICollectionView`
 minus the section inset's top and bottom values.

I don't get the warning on iOS7, and all the cells display correctly there too.

I've set my collectionView frame to height 270 in the .xib and there are no insets defined. I've set my cell height to 270 in the .xib. I can print out my collectionView frame at runtime and it's 271.

Also, my collectionview is actually inside a custom tableview cell.

Any ideas? Thanks!

like image 257
m.y Avatar asked Mar 04 '14 18:03

m.y


4 Answers

Try to set self.automaticallyAdjustsScrollViewInsets = NO

This was introduced in ios7 so you might want to wrap that with an ios version check, if you are supporting ios6 and below.

like image 141
shtefane Avatar answered Nov 02 '22 16:11

shtefane


This fixed my problem! In my .xib, setting my Collection View Size Cell Size to a smaller value.

My setup is that I have this collectionview inside a custom tableview cell and I do return the height of my tableview cell programatically (depending on the content). So it could be that my warnings had to do with my collectionview not fitting inside the tableview cell. So setting the initial collectionview to a smaller value fixed it.

I was on the wrong path thinking that the problem was with my collectionview and its colletionview cell.

Maybe?

like image 24
m.y Avatar answered Nov 02 '22 15:11

m.y


self.automaticallyAdjustsScrollViewInsets = NO

actually did the trick. it also resolved my issue in swift, where the cells of a horizontal flow layout had a frame top of -32 (???) and did not fit into the collection view properly.

like image 3
helkarli Avatar answered Nov 02 '22 14:11

helkarli


I found that I had to manually set self.collectionView.collectionViewLayout.itemSize in viewWillLayoutSubviews.

- (void)viewWillLayoutSubviews {
   self.collectionView.collectionViewLayout.itemSize = CGRectMake(...);
}

Another possibility to generate the same trick would be to implement the method
collectionView:layout:sizeForItemAtIndexPath:

like image 1
mackworth Avatar answered Nov 02 '22 15:11

mackworth