首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSCollectionView项间间隙指示器有错误的高度

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

Stack Overflow用户
提问于 2019-11-28 08:30:48
回答 1查看 321关注 0票数 3

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

我希望这些项目只能水平滚动,而无需包装到下一行,只有here)似乎能够告诉NSCollectionView不要垂直滚动(灵感来自于)。

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

代码语言:javascript
复制
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?
EN

回答 1

Stack Overflow用户

发布于 2020-08-04 02:55:01

您还可能需要重写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视图调试器,在将其添加到集合视图后检查drop指示符视图。通过这种方式,您可以确保视图层次结构是正确的,并且任何问题在视觉上都是显而易见的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59084562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档