ios UICollection View 调整屏幕大小的单元格大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16272216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:23:41  来源:igfitidea点击:

UICollection View Adjust Cell Size for Screen Size

iosobjective-cconstraintsuicollectionviewuicollectionviewcell

提问by benjih555

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 UIPageControlto 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?

目前我有一个集合视图,它在 3.5 英寸屏幕尺寸下占据了我的大部分屏幕。集合视图距屏幕顶部 51 像素,距屏幕底部 20 像素。我正在使用单元格UIPageControl创建分页布局,每页一个单元格。当切换到 4 英寸大小时,使用约束我可以让集合视图扩展到新空间(保持顶部的 51 个像素和底部的 20 个像素),但单元格大小保持不变,所以现在有一堆单元格顶部和底部的空白空间。我尝试使用约束,但似乎您无法为单元格设置它们。如何扩大单元格大小以填充 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

布局是在 IB 中设置的 我想先让布局工作,然后再添加更多

iPhone 5 view. I want to fill the empty space

iPhone 5 视图。 我想填补空白

iPhone 4 view

iPhone 4 视图

回答by rdelmar

You can either use the UICollectionViewFlowLayoutmethod itemSizeproperty to set the UICollectionViewCellsize, or use the delegatemethod, collectionView:layout:sizeForItemAtIndexPath:if you need to dynamically set the sizes of the UICollectionViewCells.

您可以使用UICollectionViewFlowLayoutmethoditemSize属性来设置UICollectionViewCell大小,或者使用该delegate方法,collectionView:layout:sizeForItemAtIndexPath:如果您需要动态设置UICollectionViewCells.

回答by Waqas Haider Sheikh

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