NSScrollView 内容大小不正确

发布于 2024-10-03 23:36:58 字数 363 浏览 0 评论 0原文

我有一个 NSScrollView,它是自定义 NSView 子类的父类。子类使用 NSScrollView 的 contentSize 方法来布局其子视图。

问题是,首次启动时,NSScrollView 报告 contentSize 错误。它报告的大小比应有的大小(滚动条的宽度)大 15 像素。所以在我看来,它返回的是 contentSize 而不考虑滚动条宽度;但是,一旦我调整滚动视图的框架(通过调整大小等),内容大小就会正确报告。这似乎只是初次启动时的问题。

这应该被报告为错误吗?对此有什么好的解决方案吗?我可以使用在布局期间执行检查的肮脏方法来查看是否是第一次调用该方法,然后从内容大小中扣除 15px,但如果有更好的东西,我们将不胜感激。

I have an NSScrollView that is the parent of a custom NSView subclass. The subclass uses the NSScrollView's contentSize method in order to layout its subviews.

The issue is that upon first launch, NSScrollView reports the contentSize wrong. It reports the size as being 15px more than it should be (the width of the scroller). So it seems to me that it is returning the contentSize without taking into account the scroller width; however, as soon as I adjust the frame of the scroll view (by resizing, etc.) the content size is reported properly. It seems to be just a problem upon initial launch.

Should this be reported as a bug, and are there any good solutions to this? I could use the dirty way of performing a check during layout to see if its the first time the method has been called, and then deduct 15px from the content size, but if there's something better, that would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里梦着梦中梦 2024-10-10 23:36:58

我刚刚遇到了类似的问题,我解决它的方法是观察滚动视图的内容视图(在您的情况下是自定义 NSView)上的通知 NSViewFrameDidChangeNotification。

当从滚动视图中添加/删除滚动条时,会触发此通知,此时您可以重新定位视图的内容。

I just came across a similar problem and the way I solved it was to observe the notification NSViewFrameDidChangeNotification on the Scroll view's content view (in your case the custom NSView).

This notification gets triggered when a scrollbar is added/removed from the scroll view, at which point you can reposition your view's content.

零度℉ 2024-10-10 23:36:58

您什么时候检查内容大小?它是在从 nib 文件实例化您的视图时发生的,还是在 nib 完全取消归档之后发生的?

我可能会做的是从 -viewDidMoveToWindow:-awakeFromNib 调用您的布局方法

When are you checking the content size? Is it happening as your view is being instantiated from the nib file, or after the nib has been fully unarchived?

What I would probably do is invoke your layout method from -viewDidMoveToWindow: or -awakeFromNib

╰◇生如夏花灿烂 2024-10-10 23:36:58

您是否使用自动隐藏/显示滚动条的选项?

Are you using the option to hide/show automatically the scrollbars?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文