How would I create custom scroll bars with cocoa?
There are two types of scroll bars: vertical and horizontal.
In the Menu bar, click Apple Menu > System Preferences. Click General. Next to the "Show scroll bars" heading, select "Always."
We call those overlay scrollbars and they are either partially or fully transparent while sitting on top of the page content. In other words, unlike classic scrollbars that take up physical real estate on the screen, overlay scrollbars sit on top of the screen content.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With