所有屏幕尺寸的 CollectionView Cell 动态高度 [英] CollectionView Cell dynamic height for all screensizes

查看:40
本文介绍了所有屏幕尺寸的 CollectionView Cell 动态高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我的 collectionview 单元格包含整个屏幕的宽度和高度,我已将其设置为水平滚动并使其对所有屏幕尺寸都是动态的,我正在使用此方法

In my project, my collectionview cell comprises of the entire screen width and height, i've set it to horizontal scrolling and for making it dynamic for all screensizes, i am using this method

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let height: CGFloat = view.frame.size.height
    let width: CGFloat = view.frame.size.width

        return CGSize(width: collectionView.bounds.size.width, height: collectionView.bounds.size.height)

它工作得很好,并处理所有屏幕尺寸的宽度和高度,但问题是在滚动时它是从右边缘切割屏幕,我尝试使用 uicollectionviewdelegateflowlayout 的不同方法 viewDidLayoutSubViews() 和它也照顾屏幕尺寸,但仍然存在相同的问题.第一张图像是原始单元格应该如何显示.

its working perfectly fine and takes care of the width and height of all screensizes but the problem is that on scrolling it is cutting of the screen from the right edge, I tried implementing it with a different method viewDidLayoutSubViews() of uicollectionviewdelegateflowlayout and it too takes cares of the screensizes but still has the same issue.The first image is the original cell how it should appear.

下一张图片是它如何在滚动时切断右边缘.最初我有静态 numberofIteminSection 并在每次滑动时不断切割.这是第三次或第四次滑动的结果.我启用了分页,所以我不知道这里有什么问题.

the next image is how it cuts off the right edge on scrolling. Initially i've static numberofIteminSection and keeps on cutting with every swipe. This one is the result of third or fourth swipe. I have enabled pagination so i dont know whats the issue here.

我已经检查了关于堆栈溢出的各种帖子,但似乎没有任何帮助,我被卡住了.任何帮助将不胜感激.

I have checked various posts on stack overflow regarding this but nothing seems to help and i am stuck. Any help would be appreciated a lot.

推荐答案

您需要在 sizeForItemAtIndexPath 中检查屏幕尺寸,例如:

You need to check the screen sizes in sizeForItemAtIndexPath like:

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

        let screenSize: CGRect = UIScreen.main.bounds
        let screenWidth = screenSize.width

        if screenWidth == 414{
            return CGSize(width: collectionViewTable.bounds.size.width - 4, height: collectionViewTable.bounds.size.height - 50)

        }else if screenWidth == 375{

            return CGSize(width: collectionViewTable.bounds.size.width - 4, height: collectionViewTable.bounds.size.height - 50)
        }else{

            return CGSize(width: collectionViewTable.bounds.size.width - 4, height: collectionViewTable.bounds.size.height - 50 )
        }
    }

同时添加这些功能并根据您的需要进行自定义:

Add these functions also and customise according to your need:

func collectionView(_ collectionView: UICollectionView,
                            layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
            return  UIEdgeInsetsMake( 5,  14, 5,  14)
        }
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
            return 4;
        }

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 4;
    }

如果这不起作用,则更改 insetForSectionAt 中的 UIEdgeInsetsMake.

If this doesn't work then change UIEdgeInsetsMake in insetForSectionAt.

这篇关于所有屏幕尺寸的 CollectionView Cell 动态高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆