Question

I'm trying to add a button to a view programmatically and I'd like it to stay "pushed in" when clicked, until it is clicked again. From what I've read it seems like the button to use for this would be NSPushOnPushOffButton. Here's Apple's discription for it:

The first click both highlights and causes the button to be “pushed in” if the button is bordered; a second click returns it to its normal state. This option is called “Push On Push Off” in Interface Builder’s Button Inspector.

It wasn't working in my program so I made a SSCCE, which I'll include below. My problem is the button isn't staying in when clicked, but rather popping back out like a normal button. I'm not sure if I'm missing something about how this button is supposed to perform or if I'm simply implementing it incorrectly (This is the first time I've tried to do a UI programmatically), but other button types such as NSToggleButton work so unless there is something special about NSPushOnPushOffButton I don't see where I went wrong. if someone could suggest a better solution or explain where I went wrong that would be great. Thanks!

#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSRect buttonFrame = NSMakeRect(0,0,200,200);
    NSButton *button = [[NSButton alloc]initWithFrame:buttonFrame];
    [self.window.contentView addSubview:button];
    [button setButtonType:NSPushOnPushOffButton];
}

@end
Was it helpful?

Solution

It seems that you have to set a "bezel style", for example

[button setBezelStyle:NSRegularSquareBezelStyle];

After adding that to your sample code, it worked as expected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top