How would I create custom scroll bars with cocoa?
+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
2009-12-28 05:40:04
FWIW, named images never go away. NSImage keeps them in a global pool. The -retain doesn't hurt anything, though.
NSResponder
2009-12-29 06:02:51
+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
2009-12-28 05:46:31