如何更改NSTableView的滚动条的外观

时间:2014-04-07 22:33:58

标签: objective-c macos cocoa nstableview

我想更改NSTableView的垂直滚动条的外观。具体来说,我想改变它的宽度以及旋钮。

我试图通过继承NSScroller并覆盖drawRect和drawKnob来做到这一点......但到目前为止还没有结果......

非常感谢任何形式的帮助!

1 个答案:

答案 0 :(得分:3)

所以...经过一些实质性的研究后,我找到了一个解决方案(解决方法)

为了改变我的滚动条的外观,我将子类化NSScroller并覆盖了一些方法:

#import "HSObjectLibraryScroller.h"

@implementation HSObjectLibraryScroller

- (id)initWithFrame:(NSRect)frameRect;
{
   if (self = [super initWithFrame:frameRect])
   {}
   return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
   CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
   CGContextSetRGBFillColor(context, 64.0f/255.0f,64.0f/255.0f,64.0f/255.0f,1.0);
   CGContextFillRect(context, NSRectToCGRect(dirtyRect));

   [self drawKnobSlot];
   [self drawKnob];
}

- (void)drawKnobSlot;
{
    NSRect slotRect = [self rectForPart:NSScrollerKnobSlot];
    NSRect r = NSMakeRect(slotRect.origin.x, slotRect.origin.y, 4, slotRect.size.height);
    NSImage* slotTop = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"top_slider"
                                                                                            ofType:@"png"]];

    NSImage* slotVertFill = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"centerVert_slider"
                                                                                                      ofType:@"png"]];

    NSImage* slotBottom = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Bottom_slider"
                                                                                             ofType:@"png"]];

    NSDrawThreePartImage(r, slotTop, slotVertFill, slotBottom, YES, NSCompositeSourceOver, 1, NO);

}
- (void)drawKnob;
{
    NSRect knobRect = [self rectForPart:NSScrollerKnob];
    NSRect r = NSMakeRect(knobRect.origin.x, knobRect.origin.y, 4, knobRect.size.height);
    [[NSColor colorWithCalibratedRed:255.0f/255.0f green:197.0f/255.0f blue:84.0f/255.0f alpha:1.0f] set];
    NSRectFill(r);
}
@end

这个实现给了我以下结果:

enter image description here