如何使屏幕大小的UICollectionViewFlowLayout itemSize动态化

时间:2023-01-22 22:58:42

I am working programmatically (no storyboard) and am having trouble making layout.itemSize dynamic for different screen sizes. I get this error message:

我正在以编程方式工作(没有故事板),并且无法为不同的屏幕尺寸制作layout.itemSize动态。我收到此错误消息:

"UICollectionView must be initialised with a non-nil layout parameter"

“必须使用非零布局参数初始化UICollectionView”

with the following code in my implementation file:

在我的实现文件中使用以下代码:

- (instancetype)init 
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

    CGSize size = self.collectionView.bounds.size;

    layout.itemSize = CGSizeMake(size.width, size.height);

    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];

    layout.minimumLineSpacing = 100.0;

    layout.headerReferenceSize = CGSizeMake(0.0, 50.0);

    return (self = [super initWithCollectionViewLayout:layout]);
}

I don't get the error message if I use "100,100" for example for layout.itemSize. Is there a way to make it dynamic though?

如果我使用“100,100”作为layout.itemSize,我不会收到错误消息。有没有办法让它变得动态?

I am new to Objective-C so would appreciate any help on what I am doing incorrectly.

我是Objective-C的新手所以非常感谢我对错误做的任何帮助。

2 个解决方案

#1


16  

Need to use the flow delegate method:

需要使用flow delegate方法:

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize cellSize = CGSizeMake(width, height);

    return cellSize;
}

You specify the width and height float values to whatever you want.

您可以根据需要指定宽度和高度浮点值。

For example, lets say your CollectionView is vertical and you want a short header view, a big middle view and a small footer view, then you could do something like:

例如,假设您的CollectionView是垂直的,并且您想要一个简短的标题视图,一个大的中间视图和一个小的页脚视图,那么您可以执行以下操作:

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize cellSize;

    cellSize.width = self.view.bounds.size.width;

    if(indexPath.row == 0)
    {
        // header view height
        cellSize.height = 100;
    }
    else if(indexPath.row == 1)
    {
        // body view height
        cellSize.height = 500;
    }
    else
    {
        // assuming number of items is 3, then footer view is last view
        // footer view height
        cellSize.height = 100;
    }

    return cellSize;
}

#2


1  

add following delegate method to your collection view

将以下委托方法添加到集合视图中

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
 {
UIImage *image;
long row = [indexPath row];

image = [UIImage imageNamed:_carImages[row]];

return image.size;
 }

here a link which help you enter link description here

这里有一个链接,可以帮助您在此输入链接描述

#1


16  

Need to use the flow delegate method:

需要使用flow delegate方法:

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize cellSize = CGSizeMake(width, height);

    return cellSize;
}

You specify the width and height float values to whatever you want.

您可以根据需要指定宽度和高度浮点值。

For example, lets say your CollectionView is vertical and you want a short header view, a big middle view and a small footer view, then you could do something like:

例如,假设您的CollectionView是垂直的,并且您想要一个简短的标题视图,一个大的中间视图和一个小的页脚视图,那么您可以执行以下操作:

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize cellSize;

    cellSize.width = self.view.bounds.size.width;

    if(indexPath.row == 0)
    {
        // header view height
        cellSize.height = 100;
    }
    else if(indexPath.row == 1)
    {
        // body view height
        cellSize.height = 500;
    }
    else
    {
        // assuming number of items is 3, then footer view is last view
        // footer view height
        cellSize.height = 100;
    }

    return cellSize;
}

#2


1  

add following delegate method to your collection view

将以下委托方法添加到集合视图中

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
 {
UIImage *image;
long row = [indexPath row];

image = [UIImage imageNamed:_carImages[row]];

return image.size;
 }

here a link which help you enter link description here

这里有一个链接,可以帮助您在此输入链接描述