ios 如何根据屏幕大小使 UICollectionViewFlowLayout itemSize 动态化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26901286/
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
How to make UICollectionViewFlowLayout itemSize dynamic for screen size
提问by Brittany
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 的新手,所以如果对我做错的事情有任何帮助,我将不胜感激。
回答by Zhang
Need to use the flow delegate method:
需要使用流委托方法:
- (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;
}
回答by Supriya Karanje
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
这里有一个链接,可帮助您在此处输入链接描述