Rounded rect on NSView that clips all containing subviews

前端 未结 3 2180
傲寒
傲寒 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 04:53

    You can do it in the interface builder without subclassing adding User Defined Runtime Attributes"

    0 讨论(0)
  • 2021-02-07 04:54

    Have you tried clipping with layers?

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


    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.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题