How can I get NSScrollView to respect clipping path

I am trying to create an NSScrollView with cropped corners similar to a Twitter application:

enter image description here

I have a subclass of NSScrollView that has added the following code:

 - (void)drawRect:(NSRect)dirtyRect { NSBezierPath *pcath = [NSBezierPath bezierPathWithRoundedRect:[self bounds] xRadius:kDefaultCornerRadius yRadius:kDefaultCornerRadius]; [path setClip]; [super drawRect:dirtyRect]; } 

I expected the contents of the NSScrollView to have rounded corners, but did not respect the cropped path. How can i do this?






UPDATE AND CONFIRMATION
I know how to create a custom NSScroller , I know how to make it transparent overlay. All I ask is to make the NSScrollView clip its corners, including everything it contains. NSScrollView is inside an NSView that has a background that can change, which means that overlaying the view to fake rounded corners is not an option.

+2
source share
3 answers

You can apply a mask to the view layer:

 [myScrollView setWantsLayer: YES]; [myScrollView layer].mask = ...; 

The mask is another CALayer . So in this case, you will create a CALayer , set its background color to opaque, set its borders according to the scrolling and give it a cornerRadius , say 8.0 .

The result will be that the scroll view and all its contents will be masked in roundrect with an angular radius of 8px.

+4
source

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]; 
+6
source

Did you try to override

 - (BOOL)isOpaque { return NO; } 

And set the scroll view -setDrawsBackground: to NO and just leave the view without cropping and just draw the corners using [NSColor clearColor] , as this will also clear the foreground color and mimic the rounding effect.

+3
source

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


All Articles