2

我想改变它的颜色,但我不确定是继承 NSScrollView 还是 NSClipView。或者,如果角落可以作为常规 NSView 插入。

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

我不需要代码。只是提示如何做到这一点。

4

3 回答 3

1

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
于 2010-08-05T10:10:21.947 回答
0

有点奇怪,但只是继承 NSScrollView 并用 super.drawRect() 覆盖绘制使我的 NSScrollView(不是)用白色填充那个角落。我对其进行了 2 次测试以确保,因为它没有多大意义。

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()
        }
    }
}
于 2016-05-13T01:38:55.660 回答
0

更新了奥斯汀答案的 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()
        }
    }
}

于 2020-05-07T15:41:31.827 回答