Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clipping rounded corners on a NSScrollView

I have a simple custom borderless NSWindow subclass which has a rounded rectangle shape.

In the content view of this window, I've added an NSScrollView.

How do I get the NSScrollView to clip its document view to the rounded rectangle shape of the NSWindow?

I've tried subclassing the NSScrollView, overriding drawRect: and adding a clipping path before calling super. I've also tried subclassing the document view and the clip view with the same technique but I cannot get it to clip.

BTW, this is on Lion with the elastic scrolling behaviour.

like image 970
firstresponder Avatar asked Oct 10 '22 00:10

firstresponder


1 Answers

After much fiddling, I just discovered that NSScrollView's can be made to have rounded corners by simply giving it a backing layer and setting that layer's corner radius provided you also do the same to it's internal NSClipView. Both are required, which now makes sense, since it's the clip view that actually provides the viewable window into the NSScrollView's document view.

NSScrollView * scrollView = ...;

// Give the NSScrollView a backing layer and set it's corner radius.
[scrollView setWantsLayer:YES];
[scrollView.layer setCornerRadius:10.0f];

// Give the NSScrollView's internal clip view a backing layer and set it's corner radius.
[scrollView.contentView setWantsLayer:YES];
[scrollView.contentView.layer setCornerRadius:10.0f];
like image 56
Dalmazio Avatar answered Oct 13 '22 12:10

Dalmazio