NSScrollview and transparent, overlay NSScroller subclasses
Asked Answered
P

3

8

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?

Polychasium answered 9/3, 2011 at 20:32 Comment(0)
P
9

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

Polychasium answered 9/3, 2011 at 22:5 Comment(1)
Thanks for mentioning about getting rid of the drawing that had to do with the track! I was trying to set it to a clear color but it was messing everything up. Removed it and all's good! Thanks!Psychotechnology
D
1

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?

Dairy answered 26/5, 2011 at 21:44 Comment(1)
This fixed an odd problem I was having with my own transparent scrollbar: it would sometimes flash white when it first appeared, then draw correctly a second or two later.Fiction
L
-2

What's about this color: [NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.0 alpha:0.0] I think you have to modify the sizes with the interface builder.

Lithium answered 9/3, 2011 at 20:42 Comment(1)
clearColor is the same. The problem is not in the sizes, but the behavior of NSScroller. The standard behavior is to push the content to the side and draw the bar, I want the content to not be pushed aside, and the bar to draw on top.Polychasium

© 2022 - 2024 — McMap. All rights reserved.