Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSScrollview and transparent, overlay NSScroller subclasses

I have made a slick NSScroller subclass, but can't figure out how to make it overlay on top of the NSScrollView instead of pushing the documentView aside.

enter image description here

Here you can see the background of a NSCollectionView that I wish to make 100% wide, and have the scroller sit along top. Currently, I have to set a white background to the scroller because drawing with a clearColor is not showing as transparent, but as black.

Am I going about this the wrong way? Am I missing something obvious here? How can I achieve the behavior of a transparent-tracked NSScroller that sits atop a NSScrollView's contents?

like image 620
coneybeare Avatar asked Mar 09 '11 20:03

coneybeare


2 Answers

I was able to get the positioning by implementing tile in the subclass OF NSSCROLLVIEW

- (void)tile {
    [super tile];
    [[self contentView] setFrame:[self bounds]];
}

And it turns out that trying to draw clear color was the problem to begin with. In the scroller subclass, I just omitted any drawing that had to do with the slider track completely BY OVERRIDING DRAWRECT: OF THE NSSCROLLER SUBCLASS, LIKE SO:

- (void)drawRect:(NSRect)dirtyRect
{
    [self drawKnob];
}

enter image description here

like image 69
coneybeare Avatar answered Oct 12 '22 13:10

coneybeare


Note that for this to work properly, you MUST enable layer-backing for the scrollView!

That is, call:

[scrollViewInstance setWantsLayer:YES];

or set it in Interface Builder.

If you don't do this, the scrollView's contentView will draw ON TOP OF the scrollers. Also: you should be aware that what you're doing is essentially overlapping two NSViews (NSScroller on top of NSScrollView --- both inherit from NSView.) Unlike UIViews on iOS, overlapping NSViews on OS X is not officially supported by any current version of the OS (10.6 down). Turning on CALayers seems to make it work, but it's still something to bear in mind. Of course, turning on layers can seriously kill drawing performance.

See this SO question for more detail: Is there a proper way to handle overlapping NSView siblings?

like image 40
Bryan Avatar answered Oct 12 '22 13:10

Bryan