ios 动态设置 UICollectionView 的 ContentSize

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17796583/
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-31 00:27:28  来源:igfitidea点击:

Dynamically Set ContentSize of UICollectionView

iosobjective-cuicollectionview

提问by jac300

I have a UICollectionViewthat has been created programatically. 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.

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

For example, if the UICollectionViewhas 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.

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

I have attempted to accomplish this by following the Apple docs here, overriding the collectionViewContentSizemethod.

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

Although this method does return a valid CGSizewhen it is called, when I instantiate the collection view, its frameis always set to zero.

尽管此方法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 collectionViewContentSizemethod.

此外,当我为 明确定义一个静态框架时UIcollectionView,它会按预期创建,所以我知道我唯一的问题是使用该collectionViewContentSize方法。

So my question is, how can I dynamically set the height of my UICollectionView?

所以我的问题是,如何动态设置我的高度UICollectionView

采纳答案by Caleb

A collection view isa scroll view, so your -collectionViewContentSizemethod is determining the size of the content, not the size of the overall view. You'll need to set the collection view's boundsor frameproperty to set the size of the collection view itself.

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

You might also want to set it's scrollEnabledproperty to NOwhile you're at it.

您可能还想在使用时将其scrollEnabled属性设置NO为。

回答by Sankalp S Raul

Use the sizeForItemAtIndexPath:method.

使用sizeForItemAtIndexPath:方法。

In 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);
}