Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollection View Adjust Cell Size for Screen Size

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

like image 322
benjih555 Avatar asked Apr 29 '13 06:04

benjih555


2 Answers

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.

like image 56
rdelmar Avatar answered Oct 23 '22 15:10

rdelmar


CollectionView layout Delegate that will help you to resize your Cell as you need.
- (CGSize)collectionView:(UICollectionView *)collectionView 
                          layout:(UICollectionViewLayout*)collectionViewLayout 
          sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
like image 32
Waqas Haider Sheikh Avatar answered Oct 23 '22 16:10

Waqas Haider Sheikh