首页 > 解决方案 > NSCollectionView 项目间间隙指示器的高度错误

问题描述

我正在为 macOS 实现一个应用程序,在该应用程序中我使用 anNSCollectionView作为一种时间线。为此,我使用自定义子类,NSCollectionViewFlowLayout原因如下:

  1. 我希望项目只能水平滚动而不换行到下一行
  2. 只有NSCollectionViewFlowLayout似乎能够告诉NSCollectionView不要垂直滚动(来自这里的灵感)

然而,所有这些工作都很顺利:我现在正在尝试实现项目的拖放重新排序。这也有效,但我注意到项目间差距指示器(蓝线)没有正确显示,即使我确实返回了正确的尺寸。我计算它们如下:

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

标签: swiftmacosdrag-and-dropnscollectionviewnscollectionviewlayout

解决方案


您可能还需要覆盖 layoutAttributesForDropTarget(at pointInCollectionView: NSPoint) -> NSCollectionViewLayoutAttributes.

文档提到此方法provides a bounding box for the gap, that will be used to position and size a drop target indicator(视图)返回的属性的框架。

您可以在拖放代码中设置断点,在拖放指示器被绘制之后(例如collectionView(:acceptDrop:indexPath:dropOperation:),然后在拖放会话期间遇到断点时,运行 Xcode 视图调试器以检查拖放指示器视图添加到集合视图。通过这种方式,您可以确保视图层次结构是正确的,并且任何问题在视觉上都很明显。


推荐阅读