swift - 单元格之间的空间 UICollectionViewLayout

标签 swift uicollectionview cell uicollectionviewlayout

我目前正在使用完全用 Swift 编码的 TreeMap 框架布局:https://github.com/yahoo/YMTreeMap

我创建了自己的 Layout 类,以便像这样自定义单元格:

import UIKit

class Layout: UICollectionViewLayout {

    var rects = [CGRect]() {
        didSet {
            invalidateLayout()
        }
    }

    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }

    override var collectionViewContentSize: CGSize {
        return self.collectionView?.frame.size ?? .zero
    }

    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        let attrs = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            attrs.frame = rects[indexPath.item]
        return attrs
    }

    open override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

        var index = 0

        return rects.compactMap { (itemRect: CGRect) -> UICollectionViewLayoutAttributes? in
            let attrs = rect.intersects(itemRect) ?
                self.layoutAttributesForItem(at: IndexPath(item: index, section: 0)) : nil

            index += 1

            return attrs
        }

    }
}

但是我无法在单元格之间插入空格。我尝试了很多东西,比如 minimumLineSpacingForSectionAt 但没有成功......

这是我拥有的:digitalblend.fr/today.png这就是需要的:digitalblend.fr/needed.png

有什么想法吗?非常感谢!

最佳答案

您可能想要更改定义自定义 UICollectionViewCell 布局的约束。请参阅 YMTreeMap 附带的示例。在目标 YMTreeMap-iOS 中,通过更改 override init(frame: CGRect) 中的以下代码行:

    colorView.widthAnchor.constraint(equalTo: contentView.widthAnchor, constant: -1.0).isActive = true
    colorView.heightAnchor.constraint(equalTo: contentView.heightAnchor, constant: -1.0).isActive = true

    colorView.widthAnchor.constraint(equalTo: contentView.widthAnchor, constant: -8.0).isActive = true
    colorView.heightAnchor.constraint(equalTo: contentView.heightAnchor, constant: -8.0).isActive = true

你可以得到这个 View :

enter image description here

关于swift - 单元格之间的空间 UICollectionViewLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52160485/

相关文章:

java - 在不删除以前的单元格的情况下将数据添加到excel中的新单元格

ios - 如何快速安排相同的本地通知

ios - 如何在删除单元格后重新加载 CollectionView

arrays - 字符串元胞数组 - 在 Matlab 中检查元素

ios - UICollectionView多个单元格(UICollectionViewCell)在Objective-C中闪烁同步

ios - UICollectionView - 滚动到第一个单元格

c# - 我想从 C# 创建 xlsx ( Excel ) 文件

ios - 带有 SwiftLoading 的 NSTimer

swift - 单击网络链接时,深度链接 URLSchema 未按需要将我带到应用程序

ios - 检查两个节点相互接触时颜色是否相同