在NSView上的圆角矩形,裁剪所有包含子视图 [英] Rounded rect on NSView that clips all containing subviews

查看:1754
本文介绍了在NSView上的圆角矩形,裁剪所有包含子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个具有圆角的 NSView 子类。这个视图是一个容器,其他的子视图将被添加到它。我试图获得 NSView 的圆角以剪切所有的子视图的角落,但是不能得到它。

I am creating a NSView subclass that has rounded corners. This view is meant to be a container and other subviews will be added to it. I am trying to get the rounded corners of the NSView to clip all of the subview's corners as well, but am not able to get it.

- (void)drawRect:(NSRect)dirtyRect {
    NSRect rect = [self bounds];
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:self.radius yRadius:self.radius];
    [path addClip];

    [[NSColor redColor] set];
    NSRectFill(dirtyRect);

    [super drawRect:dirtyRect];     
}

红色就是例子。如果我添加一个子视图到rect,角落不剪裁:

The red is just for example. If I add a subview to the rect, The corners are not clipped:

如何实现?

推荐答案

尝试用图层剪切?

Have you tried clipping with layers?

self.layer.cornerRadius = self.radius;
self.layer.masksToBounds = YES;

self.layer.cornerRadius = self.radius; self.layer.masksToBounds = YES;

啊,对不起,我错过了你正在谈论NSView,而不是UIView。在所有情况下都很难剪辑NSView子视图,因为看起来大多数Cocoa标准视图都设置了自己的剪切路径。使用某些paddings布局子视图可能更容易,并且避免需要剪切。

Ah, sorry, somehow I've missed that you were talking about NSView, not UIView. It would be hard to clip NSView subviews in all cases because it seems that most of Cocoa standard views set their own clipping path. It might be easier to layout subviews with some paddings and avoid need for clipping.

这篇关于在NSView上的圆角矩形,裁剪所有包含子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆