ios - UICollectionView 具有两个 UICollectionViewFlowLayout

标签 ios swift uicollectionview uicollectionviewflowlayout

我正在使用 iOS Swift 开发一个项目。我们如何为一个collectionView分配两种不同的流程布局?在这里,我需要 UICollectionViewCell 看起来像堆栈卡,为此,我使用 CardsCollectionViewLayout(外部 pod 文件)。

myCollectionView.collectionViewLayout = CardsCollectionViewLayout()

并且运行良好。但问题是我无法根据设备宽度调整 UICollectionViewCell 宽度。为此,我需要使用 UICollectionViewDelegateFlowLayout。

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {
    let nCol = 1
    let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
    let totalSpace = flowLayout.sectionInset.left
        + flowLayout.sectionInset.right
        + (flowLayout.minimumInteritemSpacing * CGFloat(nbCol - 1))
    let size = Int((collectionView.bounds.width - totalSpace) / CGFloat(nbCol))

    return CGSize(width: size, height: 284)
}

但是当我使用CardsCollectionViewLayout作为我的collectionView布局时,我没有在上面的方法中得到回调。

或者

有没有办法用我的 UICollectionViewCell 添加堆叠卡效果。

enter image description here

请帮助我。

最佳答案

您从错误的角度看待它,如果您使用CardsCollectionViewLayout,您必须使用该布局提供的功能才能更改它。 CardsCollectionViewLayout 中有一个名为 itemSize 的属性,您可以设置该属性来定义卡片的大小:

let layout = CardsCollectionViewLayout()
collectionView.collectionViewLayout = layout
layout.itemSize = CGSize(width: YOUR_WIDTH, height: YOUR_HEIGHT)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.isPagingEnabled = true
collectionView.showsHorizontalScrollIndicator = false

关于ios - UICollectionView 具有两个 UICollectionViewFlowLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56748989/

相关文章:

iphone - 在 swift 中使用带有 NSUserDefaults 的字典

ios - 点击时如何将播放按钮更改为暂停按钮?

ios - 我如何从 UICollectionView 传递我的 id

ios - 如果在 <x> 秒内未调用给定函数,Swift 如何触发回调?

ios - Swift 3 中点击声音实现的自定义数字键盘

ios - 如何在collectionView轮廓之外显示标签

swift - 如何使用 Firebase 更新 CollectionView 模型?

ios - 使用 OpenGL ES 2 着色器重新实现的 GLImageProcessing 的性能问题

iphone - 在为 iPhone UITextView 输入时建议 # 标签(如 Twitter)

ios - 数组扩展通用 Equatable - "Cannot invoke ' = =' with ..."