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

我正在尝试通过子类化NSScroller并覆盖drawRect和drawKnob来做到这一点...但是到目前为止没有结果...

任何帮助都将受到高度赞赏!

最佳答案

所以...经过大量研究后,我找到了解决方案(解决方法)

为了改变滚动条的外观,我将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


该实现给了我以下结果:

关于objective-c - 如何更改NSTableView滚动条的外观,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22924328/

10-10 21:12