更改minItemSize后,NSCollectionView失去滚动条,直到调整视图大小为止 [英] NSCollectionView loses scrollbars after changing minItemSize until view gets resized

查看:58
本文介绍了更改minItemSize后,NSCollectionView失去滚动条,直到调整视图大小为止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSCollectionView 和一个垂直的 NSCollectionViewFlowLayout .我在其中显示矩形图标.

I have an NSCollectionView with a vertical NSCollectionViewFlowLayout. I display rectangular icons in it.

我还在UI中有一个滑块,当它改变时,我调整了 minItemSize ,然后在 collectionView:itemForRepresentedObjectAtIndexPath:中使用它来创建大小相等的滑块 NSImageView 到返回的 NSCollectionViewItem imageView .然后,我通过调用 reloadData 重新加载视图.

I also have a slider in the UI, and when that changes, I adjust the minItemSize, which I then use in collectionView:itemForRepresentedObjectAtIndexPath: to create an equally sized NSImageView to the returned NSCollectionViewItem's imageView. Then I reload the view by invoking reloadData.

然后,该视图将按预期以新大小显示图标.

After that, the view shows the icons in the new size as intended.

问题是,如果我要显示的数据超出了当前scoll视图所能容纳的范围,那么当常规系统偏好设置"设置为显示滚动条时:滚动时,重新加载后滚动条会消失集合视图,然后尝试使用触控板滚动会导致跳动,这告诉我滚动视图认为内容就是可见的内容,没有更多可滚动的内容.

The problem is, provided I have more data to show than fits into the current scoll view, that when the General System Preferences are set to Show scroll bars: When scrolling, the scrollbar disappears after reloading the collection view, and trying to scroll with the trackpad leads to a bounce that's telling me that the scroll view thinks that the content is just what's visible, nothing more to scroll.

但是,一旦我调整窗口的大小(同时也调整了内部集合视图的大小),滚动条就会回来.

But as soon as I resize the window, which also resizes the collection view inside, the scrollbars come back.

奇怪的是,如果将系统偏好设置为显示滚动条:始终,则不会出现问题,并且滚动始终有效,并且滚动条始终保持可见.

Curiously, if the System Prefs are set to Show scroll bars: Always, then the issue does not appear and the scrolling always works, and the scrollbars always remain visible.

我已经尝试了很多事情来触发滚动视图的重新计算,例如使集合视图的布局,滚动视图本身,其内容视图无效,并且还尝试将其needsLayout设置为真的.没有任何事情可以解决这个问题.

I've tried a lot of things to trigger the re-calculation of the scroll view, such as invalidating the layout of the collection view's layout, the scroll view itself, its content view, and also tried to set their needsLayout to true. Nothing makes this work.

更新:潜在的重复( NSCollectionView不会滚动超出初始可见矩形的项目), collectionView.setFrameSize: window.setFrame:建议均不适用于我的情况(我尝试在 reloadData之前和之后添加这些行).

Update: The potential duplicate (NSCollectionView does not scroll items past initial visible rect), neither the collectionView.setFrameSize: nor the window.setFrame: suggestions work for my case (I tried adding these line before and after reloadData).

即使我这样强制调整窗口大小,它也不会使滚动条退回,只有手动拖动窗口边缘才能做到:

Even if I force a resize of the window like this, it doesn't bring the scrollbars back, only a manual dragging of the window edges does:

[self.collectionView.window setFrame:NSInsetRect(self.view.window.frame, 1, 1) display:YES];

还要注意,就我而言,最初滚动有效,只是在调整大小和重新加载后无效.

Also note that in my case, initially the scrolling works, just not after resizing and reloading.

另一个发现:当我在reloadData之后调用 selectItemsAtIndexPaths:scrollPosition:时,滚动条又回来了.但仅当我为路径传递非空的 NSSet 时.而且由于我可能没有选择任何项目,所以这不是永久性的解决方案.

Another finding: When I call selectItemsAtIndexPaths:scrollPosition: after reloadData, then the scrollbar comes back. But only if I pass a non-empty NSSet for the paths. And since I may not have selected any items, this isn't a permanent solution.

如果没有其他想法,我将尝试制作一个演示项目.

If no other ideas come up, I'll try to make a demo project.

推荐答案

我已经解决了:

我不得不延迟对 reloadData 的调用,例如通过从 dispatch_async(dispatch_get_main_queue(),^ {...});的代码中调用它;

I had to delay the call to reloadData, e.g. by calling it from within the block code of dispatch_async(dispatch_get_main_queue(), ^{ ... });

在修复之前,我确实从 setSliderValue:处理程序中调用了 reloadData ,该处理程序绑定到滑块的值.看来这并不能很好地解决.

Before the fix, I did call reloadData right from the setSliderValue: handler, which is bound to the slider's value. It appears that this didn't go well together.

这篇关于更改minItemSize后,NSCollectionView失去滚动条,直到调整视图大小为止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆