C.O. C.O. - 1 month ago 14
Objective-C Question

Cocoa Mac, IB - Can an NSButton (Type Radio) be made to act like Type "Switch" while keeping bezel "Radio" in terms of clicking on/off

This is about OSX - iOS is irrelevant to me. I have created an NSButton in InterfaceBuilder and set its bezel to "Radio". The button is not checked by default. In my application I can check it of course but when clicking it again it won't be unchecked - as radio buttons usually aren't.

Now my question is is it possible to get the NSButton with a bezel of "Radio" which seems to fix the type to "Radio", too to act like it's type was "Switch" instead (the type "Switch" can't be selected when the bezel is "Radio" so I assume this will have to done by code).

The question is not if this makes sense to do :-) It is if it can be done and by what means.

EDIT : When selecting bevel "Radio" and then setting type to "Push On Push Off" or "On Off" in Interface Builder the bezel switches to "Bevel" for the button but the radio button appearance stays nonetheless as long as one does these changes in that order and touches nothing else afterwards. The button works as on / off, too.
However there is one difference to the original "Radio" bezel I try to get. I am using this button in an NSBox and I can't get it not to draw the window's background color when set to type "Push On Push Off". Here is a screen shot of the problem:



The lower element is bevel "Radio" / type "Radio" and the upper one was switched to type "Push On Push Off" like detailed above. Can I make that window-colored square go away somehow or is there a different approach to this?

EDIT 2: I tried creating an IBOutlet for the according NSButtonCell and then to use:

[myButtonCell setBackgroundColor:[NSColor clearColor]];


The alpha coponent gets ignored however and I end up with a black background.
Still no idea how to solve this...

Solution: Odd as it might seem by chance I found that when one sets the type to "Momentary Change" one click will turn the button on and the next will turn it off and the background issue detailed above is gone. No problems.

What the docs at https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSButtonCell_Class/Reference/Reference.html#//apple_ref/c/econst/NSPushOnPushOffButton mean stating for "Momentary Change": "While the button is held down, the alternate image and alternate title are displayed. The normal image and title are displayed when the button isn’t pressed." I don't know - nothing happens when holding the button...

EDIT 3: The solution above falls apart on MacOS 10.12 (Sierra) as the resulting button will never be displayed as "on" were everything worked fine until 10.11

Answer

If you want to have NSSwitchButton behavior for a button that looks like an NSRadioButton, then you can setImage: and setAlternateImage: of the NSSwitchButton to the image and alternateImage of a dummy NSRadioButton

Comments