views:

625

answers:

3

How would I create custom scroll bars with cocoa?

+2  A: 

A good start is to have a look at this article by Aaron Hillegass. link text

Daryl

darmou
+3  A: 

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
FWIW, named images never go away. NSImage keeps them in a global pool. The -retain doesn't hurt anything, though.
NSResponder
+2  A: 

The fantastic BWToolkit http://www.brandonwalkin.com/bwtoolkit/ has its own implementation of Scroll View with a different look. The source code would tell you how it's done.

Yuji