ios UICollectionview 中不同的单元格大小

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

Different cell size in UICollectionview

iphoneiosuicollectionviewuicollectionviewcell

提问by New Jango Tester

In my iphone app,I am showing images on a collection view. I fetch the images from urls,with urls I get the image size also. eg:- let's say there are two kinds of images landscape and portrait.I get value P for portrait images and L for landscape images

在我的 iphone 应用程序中,我在集合视图中显示图像。我从 urls 获取图像,使用 urls 我也得到图像大小。例如:- 假设有两种图像横向和纵向。我为纵向图像获得值 P,为横向图像获得值 L

How to customize the collectionView cell's size ?

如何自定义 collectionView 单元格的大小?

回答by Anil Varghese

Use this UICollectionViewDelegateFlowLayoutmethod

使用这个UICollectionViewDelegateFlowLayout方法

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

回答by user3124624

1) You could maintain and swap out multiple layout objects, but there's a simpler way. Just add the following to your UICollectionViewController subclass and adjust the sizes as required:

1) 您可以维护和换出多个布局对象,但有一种更简单的方法。只需将以下内容添加到您的 UICollectionViewController 子类并根据需要调整大小:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{    
    // Adjust cell size for orientation
    if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
        return CGSizeMake(170.f, 170.f);
    }
    return CGSizeMake(192.f, 192.f);
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self.collectionView performBatchUpdates:nil completion:nil];
}

Calling -performBatchUpdates:completion:will invalidate the layout and resize the cells with animation (you can just pass nil to both block params if you've no extra adjustments to perform).

调用-performBatchUpdates:completion:将使布局无效并使用动画调整单元格的大小(如果您没有额外的调整要执行,您可以将 nil 传递给两个块参数)。

2) Instead of hardcoding the item sizes in -collectionView:layout:sizeForItemAtIndexPath, just divide the height or width of the collectionView's bounds by the number of cells you want to fit on screen. Use the height if your UICollectionViewscrolls horizontally or the width if it scrolls vertically.

2) 而不是在 中对项目大小进行硬编码-collectionView:layout:sizeForItemAtIndexPath,只需将 collectionView 边界的高度或宽度除以您想要适合屏幕的单元格数量。UICollectionView水平滚动时使用高度,垂直滚动时使用宽度。

回答by Septronic

This is what I did. I first set the size of the cell based on the size of the screen (so that we can get the best number of images on each row). You may want to change the numbers to suit your cause.

这就是我所做的。我首先根据屏幕的大小设置单元格的大小(这样我们可以获得每行的最佳图像数量)。您可能希望更改数字以适合您的原因。

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
CGFloat widthOfCell =(self.view.frame.size.width-30)/2;
CGFloat heightOfCell =widthOfCell * 1.33;
CGSize returnValue = CGSizeMake(widthOfCell, heightOfCell);
returnValue.height += 5; 
returnValue.width += 5; 
return returnValue;

}

I then used an image manipulation method to determine the landscape or portrait state, and (for my app) I decided to make every image a portrait:

然后我使用图像处理方法来确定横向或纵向状态,并且(对于我的应用程序)我决定将每个图像都设为纵向:

    if (sourceImage.size.width>sourceImage.size.height) {
    sourceImage = [[UIImage alloc] initWithCGImage: sourceImage.CGImage
                                             scale: 1.0
                                       orientation: UIImageOrientationRight];
    }

If you want to have it the other way round, swap the >with <

如果你想反过来,交换><

CAVEAT: my rotating method doesn't take into consideration if the picture is pointing left or right. In other words, If an image is landscape by upside down, my code can't recognise that. I hope someone else can help you though, and I hope I haven't gone off track! :)

注意:我的旋转方法没有考虑图片是指向左还是右。换句话说,如果图像是颠倒的风景,我的代码就无法识别。我希望其他人可以帮助你,但我希望我没有跑偏!:)

回答by bikram sapkota

For swift extend the flowlayout to you viewcontroller, if you need to show dynamic images on cell make variable which hold images name into your view contorller and use this:

为了快速将流布局扩展到您的视图控制器,如果您需要在单元格上显示动态图像,请制作将图像名称保存到您的视图控制器中的变量并使用:

let imageData = ["image1","image2","image3","image4"]

extension yourViewController: UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let photo = UIImage(named: imageData[indexPath.row])
        return CGSize(width: (photo?.size.width)!, height: (photo?.size.height)!)

}

}

}