Rounded rect on NSView that clips all containing subviews

前端 未结 3 2179
傲寒
傲寒 2021-02-07 04:09

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 rounde

3条回答
  •  花落未央
    2021-02-07 05:04

    Using Core Animation layers will clip sublayers correctly.

    In your container NSView subclass:

    - (id)initWithFrame:(NSRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.layer = _layer;   // strangely necessary
            self.wantsLayer = YES;
            self.layer.masksToBounds = YES;    
            self.layer.cornerRadius = 10.0;    
        }    
        return self;
    }
    

提交回复
热议问题