App development

NSStatusItem highlight custom Button workaround!

I’m building a Menulet with multiple buttons. Since these buttons do no inherit the default behavior of getting blue when clicked, i searched for a quick solution. Since i don’t know how to implement a custom cell and had no ambition to do so, i finally found a nice and quick solution.

The following lines make shure the button won’t show a white background which is default for every buttonType but NSMomentaryChangeButton.

  1. //make sure button won’t show the “alternateImage” when selected
  2. [configureItem setButtonType:NSMomentaryChangeButton];
  3. [configureItem setBezelStyle:NSRegularSquareBezelStyle];
  4. [configureItem setBordered:NO];

Then the next few lines are called in the MenuItem’s delegate every time the menu will open or close.

  1. #pragma mark menu delegate
  2. (void)menuWillOpen:(NSMenu *)menu {
  3.         [[configureItem cell] setBackgroundColor:[NSColor selectedMenuItemColor]];
  4. }
  5. (void)menuDidClose:(NSMenu *)menu {
  6.         [[configureItem cell] setBackgroundColor:[NSColor clearColor]];
  7.     [menu release];
  8. }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s