我目前正在使用完全用 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 :
关于swift - 单元格之间的空间 UICollectionViewLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52160485/