开发者

NSScrollview and transparent, overlay NSScroller subclasses

I have mad开发者_运维百科e 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.

NSScrollview and transparent, overlay NSScroller subclasses

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?


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];
}

NSScrollview and transparent, overlay NSScroller subclasses


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜