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?
How to change the selection color of a NSSegmentedControl button
1.7k Views Asked by memis At
4
There are 4 best solutions below
1
On
You can subclass NSSegmentedCell e override the drawSegment method:
override func drawSegment(_ segment: Int, inFrame frame: NSRect, with controlView: NSView) {
var color: NSColor
if selectedSegment == segment {
color = NSColor.red
} else {
color = NSColor.white
}
color.setFill()
frame.fill()
super.drawSegment(segment, inFrame: frame, with: controlView)
}
0
On
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]
}
}
0
On
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

You need to subclass NSSegmentedCell and overwrite the following method:
From the documentation: