NSCollectionView 项目间间隙指示器的高度错误

Posted

技术标签:

【中文标题】NSCollectionView 项目间间隙指示器的高度错误【英文标题】:NSCollectionView interitem gap indicator has wrong height 【发布时间】:2020-03-23 20:34:34 【问题描述】:

我正在为 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 中的以下方法/属性:

prepare() collectionViewContentSize layoutAttributesForElements(in rect: NSRect) -> [NSCollectionViewLayoutAttributes] layoutAttributesForItem(at indexPath: IndexPath) -> NSCollectionViewLayoutAttributes? shouldInvalidateLayout(forBoundsChange newBounds: NSRect) -> Bool layoutAttributesForInterItemGap(before indexPath: IndexPath) -> NSCollectionViewLayoutAttributes?

【问题讨论】:

【参考方案1】:

您可能还需要覆盖 layoutAttributesForDropTarget(at pointInCollectionView: NSPoint) -&gt; 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 视图调试器来检查拖放将其添加到集合视图后的指示器视图。这样您可以确保视图层次结构正确并且任何问题在视觉上都很明显。

【讨论】:

以上是关于NSCollectionView 项目间间隙指示器的高度错误的主要内容,如果未能解决你的问题,请参考以下文章

NSCollectionView 中的居中项目

NSCollectionView不会滚动项目超过初始可见的rect

NSCollectionView+NSScrollView 中的鼠标悬停

透明 NSCollectionView 背景

NSCollectionView 拖放:大多数委托事件没有被调用

NSCollectionView 斯威夫特