Specific Round Corner of NSView

582 views Asked by At

I want a specific corner of NSView with radios but I can't figure it out how to do it. for all corners, I am using

class StyledButton: NSView {
let roundLayer: CALayer = CALayer()

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
    setup()
}

override init(frame: CGRect) {
    super.init(frame: frame)
    setup()
}

func setup() {
    self.wantsLayer = true
    self.layer?.addSublayer(roundLayer)
    roundLayer.frame = self.bounds
    roundLayer.cornerRadius = 3
    roundLayer.backgroundColor = NSColor.redColor().CGColor
}

}

1

There are 1 answers

0
M Afham On BEST ANSWER

You can use maskedCorners:

    layerMinXMinYCorner = bottom left corner
    layerMaxXMinYCorner = bottom right corner
    layerMaxXMaxYCorner = top right corner
    layerMinXMaxYCorner = top left corner

Here is an example to round only bottom right and top right corners:

        let rightRoundView = NSView(frame: CGRect(x: 100, y: 100, width: 128, height: 128))
        rightRoundView.wantsLayer = true
        rightRoundView.layer?.cornerRadius = 25
        if #available(OSX 10.13, *) {
            rightRoundView.layer?.maskedCorners = [.layerMaxXMinYCorner, .layerMaxXMaxYCorner]
        } else {
            // Fallback on earlier versions
        }