How to change the selection color of a NSSegmentedControl button

1.7k Views Asked by At

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

There are 4 best solutions below

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]
}
}

enter image description here

4
On

You need to subclass NSSegmentedCell and overwrite the following method:

- (NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView

From the documentation:

Returns the color the receiver uses when drawing the selection highlight.

You should not assume that a cell would necessarily want to draw itself with the value returned from selectedControlColor. A cell may wish to draw with different a selection highlight color depending on such things as the key state of its controlView.

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
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)
}