Pergunta

I want to change the appearance of the vertical scroller of my NSTableView. Specifically, I want to change the width of it as well as the knob.

I'm trying to do it by subclassing NSScroller and overwriting drawRect and drawKnob...but so far no results there...

Any kind of help is highly appreciated!

Foi útil?

Solução

So...after some substantial research I found a solution (workaround)

In order to change the look of my scroller I subclassed NSScroller and overwrote some methods there:

#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

This implementation gave me following result:

enter image description here

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top