Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Mac Scrollbars With Cocoa

How would I create custom scroll bars with cocoa?

like image 438
nanochrome Avatar asked Dec 28 '09 04:12

nanochrome


People also ask

What are the two types of scrollbars?

There are two types of scroll bars: vertical and horizontal.

How do I set up scrollbar on Mac?

In the Menu bar, click Apple Menu > System Preferences. Click General. Next to the "Show scroll bars" heading, select "Always."

What is overlay scrollbars?

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.


1 Answers

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

like image 154
dreamlax Avatar answered Sep 21 '22 01:09

dreamlax