1

On Xcode the buttons in the top right corner look like this, depending on their selected state:

Xcode dark mode

I try to replicate it and set the images in my NSSegmentedControl but I only get this

enter image description here

I have played with segButtons.selectedSegmentBezelColor = [NSColor selectedControlColor]; or segButtons.highlighted = YES; but nothing matched. I can't find any other "color" property, in the code or in the inspector.

I'm looking for a solution that would work both on dark or light mode. Thanks in advance!

EDIT:

when using the template: it gets one step closer! but still not blue (even when trying selectedSegmentBezelColor). Much better already though enter image description here


Thomas
  • 8,306
  • 8
  • 53
  • 92
  • Select One or Select Any? Are the images templates? – Willeke Jan 11 '20 at 18:45
  • @Willeke the images are mine (I actually steal them from xcode). It is Select Any – Thomas Jan 12 '20 at 04:22
  • I remember seeing years ago some documentation about how if your images are all black and white Cocoa would automatically "know" to color them in white or blue or ... – Thomas Jan 12 '20 at 04:23
  • Try template images, see [template](https://developer.apple.com/documentation/appkit/nsimage/1520017-template) and [NSImage.init(named:)](https://developer.apple.com/documentation/appkit/nsimage/1520015-init) "images requested using this method and whose name ends in the word “Template” are automatically marked as template images." – Willeke Jan 12 '20 at 06:45
  • @Willeke thanks! updated the question, looks better but still no blue... – Thomas Jan 12 '20 at 07:04
  • If the `SwitchTracking` of the segmented control is set to `selectAny` – like in Xcode – you get the blue tint color for free. – vadian Jan 12 '20 at 08:06
  • it was set on that already. I have changed the style and this ended up doing the trick. Automatic, Separated, TexturedSquare, TexturedRounded used blue, the other styles used white Thanks everyone for chiming in! It made me keep looking – Thomas Jan 12 '20 at 17:23

2 Answers2

2

The answer is a mix of @Willeke and of mine (but thanks to everyone chiming in, it helped me to not give up).

The image has to be marked as template (which doesn't seem to be possible from the UI) and to mark the segment style as automatic (or TexturedSquare or some other options but not all). Some options are available from the UI but not automatic...

-(void)windowDidLoad {
    [_segmentedCtrl imageForSegment:2].template = YES;
    _segmentedCtrl.segmentStyle = NSSegmentStyleAutomatic;
}
Thomas
  • 8,306
  • 8
  • 53
  • 92
-1

In the Attributes panel on the right of the screen, when you select the Segmented Control (in Storyboard), near the top it says "Selected Tint." That's what you're looking for. Change that to whatever color you want.

  • Hi, thanks for the answer but I can't find it. And I can't find 'tint' or anything similar in the api. Note that I'm talking about NSSegmented..., not UISegmented... (which seems to have it) – Thomas Jan 12 '20 at 04:22