swift - NSCollectionView 项目间间隙指示器的高度错误
问题描述
我正在为 macOS 实现一个应用程序,在该应用程序中我使用 anNSCollectionView
作为一种时间线。为此,我使用自定义子类,NSCollectionViewFlowLayout
原因如下:
- 我希望项目只能水平滚动而不换行到下一行
- 只有
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
:
- 准备()
- 集合视图内容大小
- layoutAttributesForElements(in rect: NSRect) -> [NSCollectionViewLayoutAttributes]
- layoutAttributesForItem(在 indexPath:IndexPath)-> NSCollectionViewLayoutAttributes?
- shouldInvalidateLayout(forBoundsChange newBounds: NSRect) -> Bool
- layoutAttributesForInterItemGap(在 indexPath:IndexPath 之前)-> NSCollectionViewLayoutAttributes?
解决方案
您可能还需要覆盖
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 视图调试器以检查拖放指示器视图添加到集合视图。通过这种方式,您可以确保视图层次结构是正确的,并且任何问题在视觉上都很明显。
推荐阅读
- python - 比较同一数据框中的两个数据框列并返回第一列中包含的文本
- python - pd.DataFrame.select_dtypes() 包含 timedelta dtype
- python - 计算图像中的绿色百分比
- amazon-web-services - Heroku 使用 AWS Route 53 重定向子域
- regex - QMake 不匹配用于发行版检测的正则表达式
- javascript - 如何使用 react-navigation 更改屏幕(使用按钮)
- c# - 如何一般地检查实体/关系
- c - 尝试遍历树广度优先但遇到错误
- python - 通过查询更新不起作用,弹性搜索python
- c++ - 错误 C2664:无法从“NvPhysicalGpuHandle”转换参数 1