我有一个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>.对我如何解决这个问题有什么建议吗?
NOTE1:这是macOS而不是iOS (尽管错误消息声明为UICollectionViewFlowLayout)。
NOTE2:即使收到警告/错误,布局宽度仍然有效,但我想找出根本的问题。
发布于 2019-04-15 17:52:13
我在问题中发布的源代码在macOS 10.14时运行良好,没有任何问题。我将以下内容添加到显示集合视图的窗口中。
// Only Mojave and after is resizable. Before that, a full sized collection view caused issues as listed
// at https://stackoverflow.com/questions/48567326/full-width-nscollectionviewflowlayout-with-nscollectionview
if(@available(macOS 10.14, *))
{
self.window.styleMask |= NSWindowStyleMaskResizable;
} // End of macOS 10.14+发布于 2018-03-23 09:08:38
这是因为您使用的是didResize事件,太晚了。当窗口开始缩小时,你的物品太宽了。试着使用:
func shouldInvalidateLayout(forBoundsChange newBounds: NSRect) -> Bool {
return true
}...when重写流布局,以重新计算所有内容。
https://stackoverflow.com/questions/48567326
复制相似问题