NSScrollView 滚动到底部

发布于 2024-10-08 20:59:44 字数 527 浏览 3 评论 0原文

我有一个包含 NSImageView 的 NSScrollView,它根据各种因素调整大小。当它调整大小时,我通常会更改图像,因此我将 NSScrollView 滚动到顶部。这很好用。但是,当我再次开始滚动 NSScrollView 时,它会移动几个像素,然后(大多数情况下)跳到滚动的底部。它跳转一次后,它会正常工作,直到我再次将滚动条移到顶部。这让我发疯。我真正做的就是这样:

[_imageView setImage: anNSImage];

NSRect frame;

NSSize imageSize = [anNSImage] size];
frame.size = imageSize;
frame.origin = NSZeroPoint;

[_imageView setFrame: frame];
[[_mainScrollview contentView] scrollToPoint: NSMakePoint(0, [_imageView frame].size.height - [_mainScrollview frame].size.height)];

I have an NSScrollView containing an NSImageView, which resizes based on various factors. When it resizes I have generally changed the image, so I scroll the NSScrollView to the top. This works fine. However, when I start to scroll the NSScrollView again, it moves a few pixels and then (most of the time) jumps to the bottom of the scroll. After it jumps once, it works as normal until I move the scroller to the top again. This is driving me insane. All I'm really doing is this:

[_imageView setImage: anNSImage];

NSRect frame;

NSSize imageSize = [anNSImage] size];
frame.size = imageSize;
frame.origin = NSZeroPoint;

[_imageView setFrame: frame];
[[_mainScrollview contentView] scrollToPoint: NSMakePoint(0, [_imageView frame].size.height - [_mainScrollview frame].size.height)];

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

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

发布评论

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

评论(6

独闯女儿国 2024-10-15 20:59:44

尼克,谢谢你的代码。
我修改了你的代码,现在运行良好。
首先,您需要将垂直滚动条滚动到顶部。其次,滚动内容视图。

    // Scroll the vertical scroller to top
    if ([_scrollView hasVerticalScroller]) {
        _scrollView.verticalScroller.floatValue = 0;
    }

    // Scroll the contentView to top
    [_scrollView.contentView scrollToPoint:NSMakePoint(0, ((NSView*)_scrollView.documentView).frame.size.height - _scrollView.contentSize.height)];

Nick, thanks for your code.
I modified your code and it works well now.
First, you need to scroll the vertical scroller to top. Second, scroll the content view.

    // Scroll the vertical scroller to top
    if ([_scrollView hasVerticalScroller]) {
        _scrollView.verticalScroller.floatValue = 0;
    }

    // Scroll the contentView to top
    [_scrollView.contentView scrollToPoint:NSMakePoint(0, ((NSView*)_scrollView.documentView).frame.size.height - _scrollView.contentSize.height)];
你与清晨阳光 2024-10-15 20:59:44

要避免此错误,您只需启用滚动条,然后设置滚动条的隐藏属性即可。由于 NSScrollers 继承自 NSView ,这只会阻止它们显示自己:

mainScrollView.hasHorizontalScroller = YES;
mainScrollView.hasVerticalScroller = YES;
mainScrollView.verticalScroller.hidden = YES;
mainScrollView.horizontalScroller.hidden = YES;

至少对我有用。不过仅在 OS 10.7.2 上进行了测试。

顺便问一下,有人有这方面的雷达吗?如果没有人告诉他们,他们就不会修复它。 :)

To circumvent this bug you simply need to enable the scrollers and then set the hidden property of the scrollers. As NSScrollers inherit from NSView this just stops them from displaying themselves:

mainScrollView.hasHorizontalScroller = YES;
mainScrollView.hasVerticalScroller = YES;
mainScrollView.verticalScroller.hidden = YES;
mainScrollView.horizontalScroller.hidden = YES;

Works for me at least. Only tested on OS 10.7.2 though.

Does anyone have a radar filed on this one btw? They won't fix it if nobody tells them. :)

永言不败 2024-10-15 20:59:44

似乎我可以使用 reflectScrolledClipView: 而不是设置滚动条的值来同步位置。前任。

[self.scrollView.contentView scrollToPoint:NSMakePoint(300, 0)];
[self.scrollView reflectScrolledClipView:self.scrollView.contentView]; // synced then

Seems I can use reflectScrolledClipView: instead of set scrollers' values to sync the position. Ex.

[self.scrollView.contentView scrollToPoint:NSMakePoint(300, 0)];
[self.scrollView reflectScrolledClipView:self.scrollView.contentView]; // synced then
风吹短裙飘 2024-10-15 20:59:44

在 macOS Sierra 上,[_scrollView.contentViewscrollToPoint:]; 存在错误并导致不稳定的闪烁。然而,使用 [_scrollView.documentViewscrollPoint:] 为我解决了所有这些问题。这也是苹果在其教程中建议的方法

on macOS Sierra, [_scrollView.contentView scrollToPoint:]; was buggy and was causing erratic flickering. However using [_scrollView.documentView scrollPoint:] cleared up all of those issues for me. This is also the suggested method by Apple in their tutorial found here.

黎夕旧梦 2024-10-15 20:59:44

我的黑客解决方案。打开“显示垂直滚动器”(就像 Chetan 所说,这就是触发该错误的原因)。将表视图发送到后面。展开表视图的左边缘,直到滚动条被其右边缘的任何内容覆盖。

My hacky solution. Turn on Show Vertical Scroller (like Chetan said, this is what triggers the bug). Send table view to back. Expand left edge of table view until the scroller is covered by whatever's to the right edge of it.

记忆で 2024-10-15 20:59:44

您设置scrollView.contentOffset = CGPointMake(0,0)。或者根据您的要求。更改图像后设置 contectoffset。

You set the scrollView.contentOffset = CGPointMake(0,0). Or according to your requirement. Set contectoffset after you change the image.

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