How to change the selection color of a NSSegmentedControl button

Viewed 1504

I am trying to find a way to change the color of the selected control button. Is this possible by subclassing the NSSegmentedControl or NSSegmentedCell or any other way? If so, can someone show me the way?

4 Answers

I did it in different way without override but just using "False color" filter. It is not perfect as it somehow change transparent of color a little bit but it is OK for me.

class RLSegmentedControl: NSSegmentedControl {
init() {
    super.init(frame: NSZeroRect)
    addFilter()
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
    addFilter()
}

func addFilter() {
    let colorFilter = CIFilter(name: "CIFalseColor")!
    colorFilter.setDefaults()
    colorFilter.setValue(CIColor(cgColor: NSColor.white.cgColor), forKey: "inputColor0")
    colorFilter.setValue(CIColor(cgColor: NSColor.black.cgColor), forKey: "inputColor1")

//        colorFilter.setValue(CIColor(cgColor: NSColor.yellow.cgColor), forKey: "inputColor0")
//        colorFilter.setValue(CIColor(cgColor: NSColor.black.cgColor), forKey: "inputColor1")

    self.contentFilters = [colorFilter]
}
}

enter image description here

When using macOS 12.2.2 or newer, you can use the new property selectedSegmentBezelColor, with 'appearances that support it' according the documentation of Apple. Style 'Rounded' for example seems to work, while 'Textured Rounded' not.

segmentedControl.selectedSegmentBezelColor = .systemOrange
Related