Space 单元格之间 UICollectionViewLayout
Space between cells UICollectionViewLayout
我目前正在使用 Swift 中完全编码的 TreeMap 框架布局:https://github.com/yahoo/YMTreeMap
我创建了自己的布局 class 以便像这样自定义单元格:
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 and this is what is need : 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
您可以获得这个视图:
我目前正在使用 Swift 中完全编码的 TreeMap 框架布局:https://github.com/yahoo/YMTreeMap
我创建了自己的布局 class 以便像这样自定义单元格:
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 and this is what is need : 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
您可以获得这个视图: