Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a transparent NSScroller

I'm subclassing NSScroller to try to make it look similar to how "scrollbars" look on iOS. I want it to just be an overlay representing the current position that is drawn OVER what is under it. For whatever reason if I override drawRect for the scroller, I get the bounds filled with white. Is there any way to prevent this so that way I can just be drawing on top of what my NSScroller sub class is over?

Edit: Drawing with clear color seems to bring me close, but it is drawing "too clear" :P It's drawing right to the desktop, when I just want to be drawing down to the window. This picture might make it more clear alt text

Any ideas?

like image 642
BarrettJ Avatar asked Nov 15 '10 02:11

BarrettJ


2 Answers

NSView subclasses do all their drawing starting at drawRect. If you override that method in a subclass, you're responsible for doing all of the drawing (whether through you're own methods or calling upon super's methods.) For example, the following code draws just the scroll nob and makes the rest of the scrollbar's frame transparent.

- (void)drawRect:(NSRect)dirtyRect {

    // Do some custom drawing...
    [[NSColor clearColor] set];
    NSRectFill(dirtyRect);

    // Call NSScroller's drawKnob method (or your own if you overrode it)
    [self drawKnob];
  }

This is more of a proof of concept than an actual code suggestion (not sure what implications doing this minimal amount of drawing has on the NSScroller.) NSScroller also has other specific drawing methods you can override / call from an overridden drawRect: method (info available from apple's docs: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSScroller_Class/Reference/Reference.html)

like image 163
Sam Avatar answered Sep 20 '22 15:09

Sam


If you leave everything out except for drawing the knob you can get a background-less scroller...

- (void) drawRect: (NSRect) dirtyRect
{
    [self drawKnob];
}
like image 45
Jason Fuerstenberg Avatar answered Sep 20 '22 15:09

Jason Fuerstenberg