如何填充NSScrollView的右下角?

发布于 2024-09-12 19:17:59 字数 392 浏览 4 评论 0原文

我想改变它的颜色,但我不确定是否要继承 NSScrollView 或 NSClipView 的子类。或者是否可以将角作为常规 NSView 插入。

显示NSScrollView 中的白色角
(来源:flickr.com

我不知道不需要代码。只是提示如何操作。

I want to change its color, but I'm not sure wether to subclass NSScrollView or NSClipView. Or if the corner can be inserted as a regular NSView.

Shows the white corner in a NSScrollView
(source: flickr.com)

I don't need code. Just a hint at how to do it.

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

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

发布评论

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

评论(3

爱给你人给你 2024-09-19 19:18:00

mekentosj 已经在 stackoverflow 上的其他地方回答了。子类是 NSScrollView。

@interface MyScrollView : NSScrollView {
}
@end


@implementation MyScrollView

- (void)drawRect:(NSRect)rect{
    [super drawRect: rect];

    if([self hasVerticalScroller] && [self hasHorizontalScroller]){
        NSRect vframe = [[self verticalScroller]frame];
        NSRect hframe = [[self horizontalScroller]frame];
        NSRect corner;
        corner.origin.x = NSMaxX(hframe);
        corner.origin.y = NSMinY(hframe);
        corner.size.width = NSWidth(vframe);
        corner.size.height = NSHeight(hframe);

        // your custom drawing in the corner rect here
        [[NSColor redColor] set];
        NSRectFill(corner);
    }
}
@end

Already answered elsewhere on stackoverflow by mekentosj. The class to subclass is NSScrollView.

@interface MyScrollView : NSScrollView {
}
@end


@implementation MyScrollView

- (void)drawRect:(NSRect)rect{
    [super drawRect: rect];

    if([self hasVerticalScroller] && [self hasHorizontalScroller]){
        NSRect vframe = [[self verticalScroller]frame];
        NSRect hframe = [[self horizontalScroller]frame];
        NSRect corner;
        corner.origin.x = NSMaxX(hframe);
        corner.origin.y = NSMinY(hframe);
        corner.size.width = NSWidth(vframe);
        corner.size.height = NSHeight(hframe);

        // your custom drawing in the corner rect here
        [[NSColor redColor] set];
        NSRectFill(corner);
    }
}
@end
执手闯天涯 2024-09-19 19:18:00

有点奇怪,但只是子类化 NSScrollView 并使用 super.drawRect() 覆盖绘制,使我的 NSScrollView (不)用白色填充那个角落。我测试了两次以确定,因为它没有多大意义。

import Cocoa

class ThemedScrollView: NSScrollView {
    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
    }
}

这也是原始答案的 Swift 变体:

import Cocoa

class ThemedScrollView: NSScrollView {

    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)

        if hasVerticalScroller && hasHorizontalScroller {
            guard verticalScroller != nil && horizontalScroller != nil else { return }

            let vFrame = verticalScroller!.frame
            let hFrame = horizontalScroller!.frame
            let square = NSRect(origin: CGPoint(x: hFrame.maxX, y: vFrame.maxY), size: CGSize(width: vFrame.width, height: hFrame.height))

            let path = NSBezierPath(rect: square)
            let fillColor = NSColor.redColor()
            fillColor.set()
            path.fill()
        }
    }
}

Kind of odd but just subclassing NSScrollView and overriding draw with super.drawRect() made my NSScrollView (not) fill in that corner with white. I tested it 2x to make sure, since it doesn't make much sense.

import Cocoa

class ThemedScrollView: NSScrollView {
    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
    }
}

Here's a Swift variation of the original answer as well:

import Cocoa

class ThemedScrollView: NSScrollView {

    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)

        if hasVerticalScroller && hasHorizontalScroller {
            guard verticalScroller != nil && horizontalScroller != nil else { return }

            let vFrame = verticalScroller!.frame
            let hFrame = horizontalScroller!.frame
            let square = NSRect(origin: CGPoint(x: hFrame.maxX, y: vFrame.maxY), size: CGSize(width: vFrame.width, height: hFrame.height))

            let path = NSBezierPath(rect: square)
            let fillColor = NSColor.redColor()
            fillColor.set()
            path.fill()
        }
    }
}
灵芸 2024-09-19 19:18:00

更新了 Austin 答案的 Swift 5.2 版本

class Themed: NSScrollView {

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        if hasVerticalScroller && hasHorizontalScroller {
            guard verticalScroller != nil && horizontalScroller != nil else { return }

            let vFrame = verticalScroller!.frame
            let hFrame = horizontalScroller!.frame
            let square = NSRect(origin: CGPoint(x: hFrame.maxX, y: vFrame.maxY), size: CGSize(width: vFrame.width, height: hFrame.height))

            let path = NSBezierPath(rect: square)
            let fillColor = NSColor.red
            fillColor.set()
            path.fill()
        }
    }
}

Updated Swift 5.2 version of Austin's answer

class Themed: NSScrollView {

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        if hasVerticalScroller && hasHorizontalScroller {
            guard verticalScroller != nil && horizontalScroller != nil else { return }

            let vFrame = verticalScroller!.frame
            let hFrame = horizontalScroller!.frame
            let square = NSRect(origin: CGPoint(x: hFrame.maxX, y: vFrame.maxY), size: CGSize(width: vFrame.width, height: hFrame.height))

            let path = NSBezierPath(rect: square)
            let fillColor = NSColor.red
            fillColor.set()
            path.fill()
        }
    }
}

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