简体   繁体   中英

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查看。我想填补空白空间

iPhone 4查看

您可以使用UICollectionViewFlowLayout方法itemSize属性来设置UICollectionViewCell大小,也可以使用delegate方法collectionView:layout:sizeForItemAtIndexPath:如果需要动态设置UICollectionViewCells的大小。

 CollectionView layout Delegate that will help you to resize your Cell as you need. 
- (CGSize)collectionView:(UICollectionView *)collectionView 
                          layout:(UICollectionViewLayout*)collectionViewLayout 
          sizeForItemAtIndexPath:(NSIndexPath *)indexPath;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM