Question

I have successfully changed the background of NSButtonCells on most of my xib windows by using the following in windowDidLoad:

[[self.generalOkButton cell] setBackgroundColor:[NSColor blueColor]];

For some reason, when I try to do this in my Preferences Window Controller (which includes a Toolbar with NSViews that get swapped out depending on which ToolbarItem gets selected) the background color of any of the buttons in any of the views does not get changed.

Now I've stepped through the code and made sure that the buttons in the code indeed have a reference (via IBOutlet) and then I've looked at the backgroundColor before I call the method to change it and afterward. It indeed is getting changed but for some reason none of the buttons in that window ever reflect that their background was changed.

Is there some reason why maybe it wouldn't work in windowDidLoad because the NSViews are getting swapped in and out of the Window? I tried moving the code that sets the background color to the method that swaps the views in and out and that still did no good.

Any ideas/suggestions appreciated. Thanks

UPDATE

Someone on irc suggested I KVO observe the NSButtonCell to see if it was getting changed back.

I set this up and I see the change happening when I set the color but that's it. Nothing ever seems to change it again....yet still the button does not show up with the right background color on the screen.

Log:

BGColor was changed: {
    kind = 1;
    new = "NSCalibratedRGBColorSpace 0 0.423529 0.792157 1";
    old = "<null>";
}

UPDATE 2

Coded up a quick example project that shows both the tabbed button color behavior and the non-tabbed. It'll be hosted on dropbox here for a limited time: https://www.dropbox.com/s/nrl71p4yk6iyp4w/TestButtonColor.zip

Was it helpful?

Solution

Figured it out. Thanks to the guys in #macdev on freenode, they noted that a line of code I was using wasn't needed:

[self.window.contentView setWantsLayer:YES];

I wasn't really using the layers so that doesn't need to be set.

Once I removed this, the button colors worked fine in that window. Note: they also said if I did need to use layers, I'd have to set the button's layer's background color to get it to work.

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