5

How would I create custom scroll bars with cocoa?

nanochrome
  • 707
  • 2
  • 12
  • 26

1 Answers1

7

Don't re-invent too much of the wheel if you don't have to. If you just want to customise the appearance of the scroll bar, it may be easier just to subclass NSScroller and override the various draw methods.

This is untested code, but it should demonstrate what you would need to do to customise the appearance of the knob if you had your own image MyKnob.png.


@interface MyScroller : NSScroller
{
    NSImage *knobImage;
}
@end




@implementation MyScroller

- (void) dealloc
{
    [knobImage release];
    [super dealloc];
}

- (id) initWithFrame:(NSRect) frame
{
    self = [super initWithFrame:frame];
    if (!self) return nil;

    knobImage = [[NSImage imageNamed:@"MyKnob.png"] retain];

    return self;
}

- (void) drawKnob
{
    // Work out where exactly to draw the knob
    NSPoint p = NSMakePoint(0.0, 0.0);

    [knobImage drawAtPoint:p fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
}

@end

dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • FWIW, named images never go away. NSImage keeps them in a global pool. The -retain doesn't hurt anything, though. – NSResponder Dec 29 '09 at 06:02