UICollectionView调整屏幕大小的单元格大小

时间:2013-04-29 06:08:54

标签: ios objective-c constraints uicollectionview uicollectionviewcell

目前我的收藏视图占据了屏幕尺寸为3.5英寸的大部分屏幕。收集视图距离屏幕顶部51个像素,距离屏幕底部20个像素。我使用UIPageControl的单元格创建分页布局,每页一个单元格。当切换到4英寸大小时,使用约束我可以使集合视图扩展到新空间(保持从顶部51个像素和从底部20个),但单元格大小保持不变,所以现在有一堆细胞顶部和底部的空白空间。我尝试使用约束,但看起来你不能为细胞设置它们。如何扩大单元格大小以填充iPhone 5上的空白区域?

集合视图代码只是

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

}

布局在IB中设置我想在添加更多

之前先让布局工作

iPhone 5 view. I want to fill the empty space

iPhone 4 view

2 个答案:

答案 0 :(得分:71)

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

答案 1 :(得分: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;