将 NSScroller 覆盖在内容上

发布于 2024-10-03 17:52:23 字数 458 浏览 0 评论 0原文

有没有办法将 NSScroller 覆盖在滚动视图的内容上(就像在 iOS 中一样)?我已经尝试过几种方法:

a) 设置滚动视图内容视图 (NSClipView) 的框架以扩展到滚动条的边界

b) 添加 NSScroller 对象作为滚动视图的子视图(位于我想要的位置)

c) 创建一个完全自定义的滚动视图并将其放置为子视图(这有效,但这意味着我需要重写所有功能NSScroller 的)

Sparrow 似乎成功地做到了这一点,而且它似乎是通过常规的 NSScroller 子类来做到这一点的(因为它响应滚动在系统偏好设置>>外观中设置)。问题并不是真正绘制滚动条,而是让它覆盖了内容。

任何建议表示赞赏:-)

Is there any way to overlay the NSScroller over the content of the scroll view (like in iOS)? I've already tried several approaches:

a) setting the frame of the scroll view content view (NSClipView) to extend into the bounds of the scroller

b) adding an NSScroller object as a subview of the scroll view (positioned where I want)

c) creating an entirely custom scroller view and placing it as a subview (this worked, but that would mean that I need to rewrite all the functionality of NSScroller)

Sparrow seems to successfully do this, and it seems to do it through a regular NSScroller subclass (seeing as it responds to the scroll settings set in System Preferences >> Appearance). It's not really drawing the scroller that's the issue, its just making it overlay the content.

Any advice is appreciated :-)

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

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

发布评论

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

评论(4

无悔心 2024-10-10 17:52:23

您可以在此处设置滚动条的自定义类。

之后,通过覆盖 NSScrollView 的 -tile 方法,您将正确放置它们。

Here's where you can set the custom class of your scrollbars.

After that, by overriding the -tile method of NSScrollView, you'll get them placed properly.

烟沫凡尘 2024-10-10 17:52:23

这是我的解决方案:
创建一个扩展 NSScroller 的 MyScroller 类

在 MyScroller.m 中

#import "MyScroller.h"


@implementation MyScroller

+(CGFloat) scrollerWidth{
    return 10;
}

+(CGFloat) scrollerWidthForControlSize:(NSControlSize)controlSize{
    return 10;
}

- (void) drawBackground:(NSRect) rect{
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:0 yRadius:0];
    [[NSColor whiteColor] set];
    [path fill];
}

- (void)drawKnob{
    [self drawBackground:[self rectForPart:0]];
    [self drawBackground:[self rectForPart:1]];
    [self drawBackground:[self rectForPart:2]];
    [self drawBackground:[self rectForPart:4]];
    [self drawBackground:[self rectForPart:5]];
    [self drawBackground:[self rectForPart:6]];


    NSRect knobRect = [self rectForPart:NSScrollerKnob];
    NSRect newRect = NSMakeRect((knobRect.size.width - [MyScroller scrollerWidth]) / 2, knobRect.origin.y, [MyScroller scrollerWidth], knobRect.size.height);
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:newRect xRadius:5 yRadius:5];
    [[NSColor grayColor] set];
    [path fill];
}
@end

:然后只需在 Interface Builder 中为 Scroller 设置自定义类即可。

Here is my solution :
Create a MyScroller class that extends NSScroller

In the MyScroller.m :

#import "MyScroller.h"


@implementation MyScroller

+(CGFloat) scrollerWidth{
    return 10;
}

+(CGFloat) scrollerWidthForControlSize:(NSControlSize)controlSize{
    return 10;
}

- (void) drawBackground:(NSRect) rect{
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:0 yRadius:0];
    [[NSColor whiteColor] set];
    [path fill];
}

- (void)drawKnob{
    [self drawBackground:[self rectForPart:0]];
    [self drawBackground:[self rectForPart:1]];
    [self drawBackground:[self rectForPart:2]];
    [self drawBackground:[self rectForPart:4]];
    [self drawBackground:[self rectForPart:5]];
    [self drawBackground:[self rectForPart:6]];


    NSRect knobRect = [self rectForPart:NSScrollerKnob];
    NSRect newRect = NSMakeRect((knobRect.size.width - [MyScroller scrollerWidth]) / 2, knobRect.origin.y, [MyScroller scrollerWidth], knobRect.size.height);
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:newRect xRadius:5 yRadius:5];
    [[NSColor grayColor] set];
    [path fill];
}
@end

Then just set the custom class for the Scroller in Interface Builder.

音盲 2024-10-10 17:52:23

我最近发布了 RFOverlayScrollView 应该可以解决您的问题:

来源:https://github.com/rheinfabrik/RFOverlayScrollView

博客文章:http://blog.rheinfabrik.de/blog /2013/01/01/介绍-rfoverlayscrollview/

I've recently released RFOverlayScrollView which should solve your problem:

Source: https://github.com/rheinfabrik/RFOverlayScrollView

Blog Post: http://blog.rheinfabrik.de/blog/2013/01/01/introducing-rfoverlayscrollview/

柏拉图鍀咏恒 2024-10-10 17:52:23

您可以将 NSScrollerView 控件上的 scrollerStyle 更改为 .overlay 以获得所需的叠加效果。

这可能会否决系统设置中滚动条可见性的系统范围设置。要在应用运行时“幸存”滚动条设置更改,您必须观察 NSScroller.preferredScrollerStyleDidChangeNotification 并重新应用叠加效果。

You can change the scrollerStyle over the NSScrollerView control to .overlay to get the desired overlay effect.

This may overrule the system wide setting in System Settings for scroll bar visibility. To 'survive' scroll bar setting changes while your app is running, you must observe the NSScroller.preferredScrollerStyleDidChangeNotification and re-apply the overlay effect.

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