如何获得NSScrollView尊重剪切路径 [英] How can I get NSScrollView to respect a clipping path
问题描述
我想让一个NSScrollView剪辑的角落,类似于Twitter的应用程序:
I am trying to make a NSScrollView with clipped corners, similar to the Twitter app:
我有一个 NSScrollView
添加了以下代码:
I have a NSScrollView
subclass which I added the following code:
- (void)drawRect:(NSRect)dirtyRect {
NSBezierPath *pcath = [NSBezierPath bezierPathWithRoundedRect:[self bounds] xRadius:kDefaultCornerRadius yRadius:kDefaultCornerRadius];
[path setClip];
[super drawRect:dirtyRect];
}
我希望 NSScrollView
有圆角,但它不尊重剪裁的路径。我如何做到这一点?
I expected the content of the NSScrollView
to have rounded corners, but it is not respecting the clipped path. How can I do this?
UPDATE&澄清
我知道如何制作一个自定义的 NSScroller
,我知道如何使其透明覆盖。我要求的是如何使 NSSCrollView
剪辑它的角落,包括它包含的一切。 NSScrollView
在一个 NSView
里面,它有一个可以改变的背景,这意味着一个视图覆盖来伪造圆角是
UPDATE & CLARIFICATION
I know how to make a custom NSScroller
, I know how to make it transparent overlay. All I am asking is how to make the NSSCrollView
clip its corners, including everything it contains. The NSScrollView
is inside a NSView
which has a background that could change, meaning a view overlay to fake the rounded corners is not an option.
推荐答案
您可以对视图的图层应用遮罩:
You can apply a mask to a view's layer:
[myScrollView setWantsLayer: YES];
[myScrollView layer].mask = ...;
掩码是另一个 CALayer
。因此,在这种情况下,您将创建一个 CALayer
,将其背景颜色设置为不透明,设置其边界以匹配scrollview,并给它一个 cornerRadius $
The mask is another CALayer
. So in this case you'd create a CALayer
, set its background colour to opaque, set its bounds to match the scrollview, and give it a cornerRadius
of, say, 8.0
.
结果将是滚动视图和它的所有内容将被掩盖为一个圆角半径为8px的圆角。
The result would be that the scroll view and all its contents would appear to be masked to a roundrect with a corner radius of 8px.
这篇关于如何获得NSScrollView尊重剪切路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!