34

Currently i have a collection view which takes up most of my screen at the 3.5 inch screen size.The collection view is 51 pixels from the top and 20 pixels from the bottom of the screen. I am using the cells with UIPageControl to create a paged layout, with one cell per page. When switching to the 4 inch size, using constraints i can get the collection view to expand into the new space (maintaining the 51 pixels from the top and 20 from bottom), but the cell size stays the same, so now there is a bunch of empty space at the top and bottom of the cell. I tried using constraints, but it appears you can't set them for cells. How do i expand the cell size bigger to fill the empty space on the iPhone 5?

The collection view code is just

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
              cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
return cell;

}

The layout is setup in IB I want to get the layout working first before I add more

iPhone 5 view. I want to fill the empty space

iPhone 4 view

benjih555
  • 931
  • 2
  • 10
  • 25
  • 1
    can you plz put code snippet that you have used? – Swapnil Apr 29 '13 at 06:41
  • 1
    @iSwap its nothing much I just made a basic collection view, with the layout setup in interface builder. The collection view works fine, I just need to figure out how to make the cell take up the empty space on the iphone 5 – benjih555 Apr 29 '13 at 06:49

2 Answers2

72

You can either use the UICollectionViewFlowLayout method itemSize property to set the UICollectionViewCell size, or use the delegate method, collectionView:layout:sizeForItemAtIndexPath: if you need to dynamically set the sizes of the UICollectionViewCells.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • 2
    @rdelmar What are the pros and cons of the two methods you stated? – Sanne May 13 '13 at 12:58
  • 7
    @Sanne, itemSize is sets the default size for the cells, and is fine if you want all cells to be the same size. Use the delegate method if you need to return different sizes for items at particular index paths. – rdelmar May 13 '13 at 14:30
7
CollectionView layout Delegate that will help you to resize your Cell as you need.
- (CGSize)collectionView:(UICollectionView *)collectionView 
                          layout:(UICollectionViewLayout*)collectionViewLayout 
          sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
  • 2
    wouldn't be bad to tell whats the deal with your code! – Alex Cio Aug 21 '14 at 11:42
  • 6
    The poster asked: *"How do i expand the cell size bigger to fill the empty space"*. He/she did *not* ask for a function name. If you are going to provide a one line function name without explaining how to use it, you should probably add it as a comment to the original question. – jww Aug 21 '14 at 11:52
  • 4
    this is a simple delegate, what else explaination is required ? and Question :: UICollection View Adjust Cell Size for Screen Size. so i think this answer have enough clarification ? 亚历山大 you tell me how would i explain it more ? – Waqas Haider Sheikh Oct 31 '14 at 07:01
  • 2
    same explaination as you use HeightForRow Delegate and return CGFloat for the required Height, in this scenrio you will return the CGSize that you want for your CollectionViewCell .. Clear ? tell me if you face any issue :) – Waqas Haider Sheikh Oct 31 '14 at 07:06
  • 1
    Hey posters before, RTFM does not help anymore? – pronebird Nov 23 '14 at 14:29
  • 1
    @Andy if you're facing any issue you can ask with me. – Waqas Haider Sheikh Jul 14 '15 at 05:56