我正在为macOS实现一个应用程序,其中使用NSCollectionView
作为时间轴。为此,出于以下原因,我使用NSCollectionViewFlowLayout
的自定义子类:
NSCollectionViewFlowLayout
似乎能够告诉NSCollectionView
不垂直滚动(来自here的启示]所有这些都运行顺利,但是:我现在正在尝试对项目进行拖放重新排序。这也可以,但是我注意到项目间间隙指示器(蓝线)没有正确显示,即使我确实返回了正确的尺寸。我计算它们如下:
override func layoutAttributesForInterItemGap(before indexPath: IndexPath) -> NSCollectionViewLayoutAttributes?
{
var result: NSCollectionViewLayoutAttributes? = nil
// The itemLayoutAttributes dictionary is created in prepare() and contains
// the layout attributes for every item in the collection view
if indexPath.item < itemLayoutAttributes.count
{
if let itemLayout = itemLayoutAttributes[indexPath]
{
result = NSCollectionViewLayoutAttributes(forInterItemGapBefore: indexPath)
result?.frame = NSRect(x: itemLayout.frame.origin.x - 4, y: itemLayout.frame.origin.y, width: 3, height: itemLayout.size.height)
}
}
else
{
if let itemLayout = itemLayoutAttributes.reversed().first
{
result = NSCollectionViewLayoutAttributes(forInterItemGapBefore: indexPath)
result?.frame = NSRect(x: itemLayout.value.frame.origin.x + itemLayout.value.frame.size.width + 4, y: itemLayout.value.frame.origin.y, width: 3, height: itemLayout.value.size.height)
}
}
return result
}
根据传递给该方法的[文档,indexPath
,它可以在0到集合视图中的项目数之间]。如您所见,我为项目间间隙指示器返回了一个矩形,该矩形应为3像素宽,且与项目的高度相同。尽管指示器以正确的宽度显示在正确的x位置,但是无论我经过什么高度,它都只有2个像素高。
我该如何解决?
注意:如果我暂时将布局更改为NSCollectionViewFlowLayout
,指示符将正确显示。
我覆盖NSCollectionViewFlowLayout
中的以下方法/属性: