我能够做的是提供我的 NSWindow 的自定义子类:
@implementation ELGRoundWindow
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:bufferingType defer:flag];
if ( self )
{
[self setStyleMask:NSBorderlessWindowMask];
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
}
return self;
}
- (void) setContentView:(NSView *)aView
{
aView.wantsLayer = YES;
aView.layer.frame = aView.frame;
aView.layer.cornerRadius = 20.0;
aView.layer.masksToBounds = YES;
[super setContentView:aView];
}
@end
然后在 IB 中,我将内容视图的类更改为 ELGRoundView:
@implementation ELGRoundView
- (void)drawRect:(NSRect)dirtyRect
{
[[NSColor colorWithCalibratedRed:0.0 green:0.5 blue:1 alpha:1] set];
NSRectFill(dirtyRect);
}
@end
我在内容视图中放置了另一个方形子视图,其中包含以下内容:
@implementation ELGSquareView
- (void)drawRect:(NSRect)dirtyRect
{
[[NSColor colorWithCalibratedRed:0.0 green:0 blue:1 alpha:1] set];
NSRectFill(dirtyRect);
}
@end
我最终得到: