我目前正在使用在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
有什么主意吗?提前非常感谢!
答案 0 :(得分:1)
您可能想要更改定义自定义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
您可以获得此视图: