全宽 NSCollectionViewFlowLayout 与 NSCollectionView

Full width NSCollectionViewFlowLayout with NSCollectionView

我有一个 NSCollectionViewFlowLayout,其中包含以下内容:

- (NSSize) itemSize
{
    return CGSizeMake(self.collectionView.bounds.size.width, kItemHeight);
} // End of itemSize

- (CGFloat) minimumLineSpacing
{
    return 0;
} // End of minimumLineSpacing

- (CGFloat) minimumInteritemSpacing
{
    return 0;
} // End of minimumInteritemSpacing

我尝试了使布局响应的方法(每当调整大小时设置宽度)。我尝试添加以下内容:

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(onWindowDidResize:)
                                             name: NSWindowDidResizeNotification
                                           object: nil];

- (void) onWindowDidResize: (NSNotification*) notification
{
    [connectionsListView.collectionViewLayout invalidateLayout];
} // End of windowDidResize:

如果我展开集合视图(将大小调整得更大),效果会很好。但是,如果我尝试折叠视图(缩小尺寸),则会出现以下异常:

The behavior of the UICollectionViewFlowLayout is not defined because:
The item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
The relevant UICollectionViewFlowLayout instance is <TestListLayout: 0x106f70f90>, and it is attached to <NSCollectionView: 0x106f76480>.

关于如何解决这个问题有什么建议吗?

注意 1:这是 macOS 而不是 iOS(即使错误消息指出 UICollectionViewFlowLayout)。

注意 2:尽管我收到 warning/error 布局宽度有效,但我想找出根本问题。

这是因为您正在使用 didResize 事件,为时已晚。在 window 开始收缩时,您的项目太宽了。尝试使用:

func shouldInvalidateLayout(forBoundsChange newBounds: NSRect) -> Bool {
    return true
}

...覆盖流程布局时,重新计算所有内容。

我在问题中发布的源代码在 macOS 10.14 上运行良好,没有任何问题。我将以下内容添加到显示集合视图的 window。

// Only Mojave and after is resizable. Before that, a full sized collection view caused issues as listed
// at 
if(@available(macOS 10.14, *))
{
    self.window.styleMask |= NSWindowStyleMaskResizable;
} // End of macOS 10.14+

我遇到了同样的问题,但就我而言,我调整了视图大小。在我更改失效上下文之前,@Giles 解决方案对我不起作用

class MyCollectionViewFlowLayout: NSCollectionViewFlowLayout {

    override func shouldInvalidateLayout(forBoundsChange newBounds: NSRect) -> Bool {
        return true
    }

    override func invalidationContext(forBoundsChange newBounds: NSRect) -> NSCollectionViewLayoutInvalidationContext {
       let context = super.invalidationContext(forBoundsChange: newBounds) as! NSCollectionViewFlowLayoutInvalidationContext
       context.invalidateFlowLayoutDelegateMetrics = true
       return context
    }
}

希望它对某人有所帮助,因为我花了几个晚上才找到解决方案