NSView子类的圆角不起作用

时间:2011-08-05 11:36:18

标签: cocoa nsview

我有一个自定义的NSView子类,我想要圆角。我在.m文件中使用以下代码:

#import "ItemImageSelectionView.h"

@implementation ItemImageSelectionView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.wantsLayer = YES;
        self.layer.frame = self.frame;
    }

    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{

    [[NSColor colorWithCalibratedRed:0.0 green:0.5 blue:1 alpha:1] set];
    NSRectFill(dirtyRect);

    [self.layer setCornerRadius:5.0];

}

@end

我使用此代码初始化视图,非常默认:

NSView *imageSelectionView = [[ItemImageSelectionView alloc] initWithFrame:CGRectMake(imageView.frame.origin.x - 2, imageView.frame.origin.y - 2, imageView.frame.size.width + 4, imageView.frame.size.height + 4)];
    [self addSubview:imageSelectionView positioned:NSWindowBelow relativeTo:imageView];

但它没有设置任何圆角!我做错了什么?

1 个答案:

答案 0 :(得分:1)

您不需要在drawrect方法中设置圆角;在init中设置它。此外,您应该在覆盖的方法中调用super方法[super drawRect:dirtyRect]。不过,我不确定您的填充自定义将如何与图层交互。您可能需要设置[self.layer setMasksToBounds:YES];