简体   繁体   English

动态设置UICollectionView的ContentSize

[英]Dynamically Set ContentSize of UICollectionView

I have a UICollectionView that has been created programatically. 我有一个UICollectionView编程方式创建的UICollectionView Upon creation of the collection view, I would like to dynamically define the collection view height based on the number of cells that it must hold. 创建集合视图后,我想根据它必须保存的单元格数动态定义集合视图高度。 I do not want to enable scrolling of the collection view itself, rather this collection view is being added as a subview to a view that is contained inside a vertical UIScrollView . 我不想启用滚动集合视图本身,而是将此集合视图作为子视图添加到包含在垂直UIScrollView的视图。

For example, if the UICollectionView has 10 UICollectionViewCells . 例如,如果UICollectionView有10个UICollectionViewCells It might have a height of 200.0f, but if it has 20 cells it might have a height of 300.0f, and so on. 它可能有200.0f的高度,但如果它有20个单元格,它的高度可能为300.0f,依此类推。

I have attempted to accomplish this by following the Apple docs here , overriding the collectionViewContentSize method. 我试图通过遵循这里的Apple文档来完成此操作,覆盖collectionViewContentSize方法。

Although this method does return a valid CGSize when it is called, when I instantiate the collection view, its frame is always set to zero. 虽然此方法在CGSize时返回有效的CGSize ,但在实例化集合视图时,其frame始终设置为零。

Here is what I have done so far: 这是我到目前为止所做的:

//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];
}

Additionally, when I explicitly define a static frame for the UIcollectionView , it is created as expected, so I know that my only issue is in using the collectionViewContentSize method. 另外,当我为UIcollectionView显式定义静态帧时,它是按预期创建的,所以我知道我唯一的问题是使用collectionViewContentSize方法。

So my question is, how can I dynamically set the height of my UICollectionView ? 所以我的问题是,如何动态设置我的UICollectionView的高度?

A collection view is a scroll view, so your -collectionViewContentSize method is determining the size of the content, not the size of the overall view. 集合视图滚动视图,因此您的-collectionViewContentSize方法正在确定内容的大小,而不是整体视图的大小。 You'll need to set the collection view's bounds or frame property to set the size of the collection view itself. 您需要设置集合视图的boundsframe属性以设置集合视图本身的大小。

You might also want to set it's scrollEnabled property to NO while you're at it. 你可能还想将它的scrollEnabled属性设置为NO

Use the sizeForItemAtIndexPath: method. 使用sizeForItemAtIndexPath:方法。

In Swift: 在Swift中:

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

In Objective-C: 在Objective-C中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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