Trimming rounded corners on an NSScrollView

I have a simple custom NSWindow subclass without a frame that has a rounded rectangular shape.

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

How can I get NSScrollView to copy its document view to the rounded rectangular shape of NSWindow?

I tried to subclass NSScrollView by overriding drawRect: and adding a clipping path before calling super. I also tried to subclass the presentation of the document and the presentation of the clip using the same technique, but I cannot get it to pin.

By the way, this is a lion with elastic scroll behavior.

+4
source share
2 answers

After long wars, I found that NSScrollView can be made rounded by just providing it with a reference layer and setting this radius to the corner of the layer, if you also do the same with it inside NSClipView . Both of them are required, which now makes sense, since this is a clip view that actually provides a visible window in the NSScrollView document NSScrollView .

 NSScrollView * scrollView = ...; // Give the NSScrollView a backing layer and set it corner radius. [scrollView setWantsLayer:YES]; [scrollView.layer setCornerRadius:10.0f]; // Give the NSScrollView internal clip view a backing layer and set it corner radius. [scrollView.contentView setWantsLayer:YES]; [scrollView.contentView.layer setCornerRadius:10.0f]; 
+4
source

IMO is even better:

 scrollView.wantsLayer = true scrollView.layer?.masksToBounds = true scrollView.contentView.wantsLayer = true scrollView.contentView.layer?.masksToBounds = true 
0
source

Source: https://habr.com/ru/post/1390971/


All Articles