Ios 动态设置UICollectionView的ContentSize

Ios 动态设置UICollectionView的ContentSize,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我有一个UICollectionView,它是通过编程创建的。在创建集合视图时,我希望根据集合视图必须包含的单元格数量动态定义集合视图的高度。我不想启用集合视图本身的滚动,而是将此集合视图作为子视图添加到垂直UIScrollView中包含的视图中 例如,如果UICollectionView有10个UICollectionViewCells。它的高度可能为200.0华氏度,但如果它有20个单元格,它的高度可能为300.0华氏度,依此类推 我试图通过遵循Apple文档,覆盖collectionVie

我有一个
UICollectionView
,它是通过编程创建的。在创建集合视图时,我希望根据集合视图必须包含的单元格数量动态定义集合视图的高度。我不想启用集合视图本身的滚动,而是将此集合视图作为子视图添加到垂直
UIScrollView
中包含的视图中

例如,如果
UICollectionView
有10个
UICollectionViewCells
。它的高度可能为200.0华氏度,但如果它有20个单元格,它的高度可能为300.0华氏度,依此类推

我试图通过遵循Apple文档,覆盖
collectionViewContentSize
方法来实现这一点

尽管此方法在调用时返回有效的
CGSize
,但在实例化集合视图时,其
frame
始终设置为零

以下是我迄今为止所做的工作:

//subclass UICollectionViewFlowLayout

@interface LabelLayout : UICollectionViewFlowLayout

@property (nonatomic, assign) NSInteger cellCount;
@property (nonatomic) UIEdgeInsets sectionInset;
@property (nonatomic) CGSize itemSize;
@property (nonatomic) CGFloat minimumLineSpacing;
@property (nonatomic) CGFloat minimumInteritemSpacing;

@end

- (id)init
{
   self = [super init];
    if (self) {
        [self setup];
    }

return self;
}

-(void)prepareLayout
{
    [super prepareLayout];
    _cellCount = [[self collectionView] numberOfItemsInSection:0];
}

- (void)setup
{
    self.sectionInset = UIEdgeInsetsMake(10.0f, 0.0f, 10.0f, 0.0f);
    self.itemSize = CGSizeMake(245.0f, 45.0f);
    self.minimumLineSpacing = 10.0f;
    self.minimumInteritemSpacing = 20.0f;
}

- (CGSize)collectionViewContentSize
{
   CGFloat collectionViewWidth = 550;
   CGFloat topMargin = 10;
   CGFloat bottomMargin = 10;
   CGFloat collectionViewHeight = (self.cellCount * (self.itemSize.height +    
   self.minimumLineSpacing*2)) + topMargin + bottomMargin;

   //THIS RETURNS A VALID, REASONABLE SIZE, but the collection view frame never gets set with it!
   return CGSizeMake(collectionViewWidth, collectionViewHeight);
 }

//create collectionView in viewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self makeLabels]; //determines the number of cells

    LabelLayout *layout = [[LabelLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];   
    self.collectionView.backgroundColor = [UIColor redColor];

    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
    [self.collectionView reloadData];
    [self.view addSubview:self.collectionView];
}
此外,当我为
UIcollectionView
显式定义静态框架时,它是按预期创建的,因此我知道我唯一的问题是使用
collectionViewContentSize
方法

因此,我的问题是,如何动态设置我的
UICollectionView
的高度?

集合视图是一个滚动视图,因此您的
-collectionViewContentSize
方法决定的是内容的大小,而不是整个视图的大小。您需要设置集合视图的
bounds
frame
属性来设置集合视图本身的大小


您可能还希望在运行时将其
scrollEnabled
属性设置为
NO

使用
sizeForItemAtIndexPath:
方法

迅速:

func collectionView(_ collectionView: UICollectionView,
    layout collectionViewLayout: UICollectionViewLayout,
    sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
    return CGSizeMake(250, 150);
}
在目标C中:

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(250, 150);
}

明白了,谢谢。最后我做了这个来让它工作:CGFloat cellHeight=45;CGFloat height=[self.labels count]*单元格高度;CGFloat宽度=550_collectionView.frame=CGRectMake(0,0,宽度,高度);使用故事板先生,你的生活会变得更轻松。@AdamWaite,请告诉我怎么做,我正在使用故事板