开发者

Rounded corners for NSView subclass not working

开发者 https://www.devze.com 2023-03-25 07:59 出处:网络
I\'ve got a custom NSView subclass which I want to have rounded corners. I use the following code in the .m file:

I've got a custom NSView subclass which I want to have rounded corners. I use the following code in the .m file:

#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

I use this co开发者_Go百科de to initialize the view, pretty default:

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];

But it doesn't set any rounded corners! What am I doing wrong?


You don't need to set rounded corners in the drawrect method; set that in the init. Also, you should probably call the super method, [super drawRect:dirtyRect] in your overrided method. I'm not sure how your fill custom will interact with the layers, though. You may need to set [self.layer setMasksToBounds:YES];

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号