Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9186181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:27:42+00:00 2026-06-17T19:27:42+00:00

I am having some troubles understanding how to wire a custom NSView for an

  • 0

I am having some troubles understanding how to wire a custom NSView for an NSMenuItem to support both animation and dragging and dropping. I have the following subclass of NSView handling the bulk of the job. It draws my icon when the application launches correctly, but I have been unable to correctly setup the subview to change when I invoke the setIcon function from another caller. Is there some element of the design that I am missing?

TrayIconView.m

#import "TrayIconView.h"

@implementation TrayIconView
@synthesize statusItem;
static NSImageView *_imageView;

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
      statusItem = nil;
      isMenuVisible = NO;
      _imageView = [[NSImageView alloc] initWithFrame:[self bounds]];
      [self addSubview:_imageView];
    }

    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
  // Draw status bar background, highlighted if menu is showing
  [statusItem drawStatusBarBackgroundInRect:[self bounds]
                              withHighlight:isMenuVisible];
}

- (void)mouseDown:(NSEvent *)event {
  [[self menu] setDelegate:self];
  [statusItem popUpStatusItemMenu:[self menu]];
  [self setNeedsDisplay:YES];
}

- (void)rightMouseDown:(NSEvent *)event {
  // Treat right-click just like left-click
  [self mouseDown:event];
}

- (void)menuWillOpen:(NSMenu *)menu {
  isMenuVisible = YES;
  [self setNeedsDisplay:YES];
}

- (void)menuDidClose:(NSMenu *)menu {
  isMenuVisible = NO;
  [menu setDelegate:nil];
  [self setNeedsDisplay:YES];
}

- (void)setIcon:(NSImage *)icon {
  [_imageView setImage:icon];
}

TrayIconView.h

#import <Cocoa/Cocoa.h>

@interface TrayIconView : NSView

{
  BOOL isMenuVisible;
}

@property (retain, nonatomic) NSStatusItem *statusItem;

- (void)setIcon:(NSImage *)icon;

@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:27:43+00:00Added an answer on June 17, 2026 at 7:27 pm

    The solution to this problem was actually outside of the view detailed here. The caller of the interface was being double instantiated on accident, thus nulling out the reference to the previously created NSView. After correcting that concern the app draws and works just fine.

    With regard to dragging, I just implemented a subclass of NSView that implemented the Cocoa draggable protocol and added it as a subview to this parent class. That allows dragging onto the currently established NSRect that contains the menubar icon.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some troubles understanding and implementing the MVC pattern. I have a
Having trouble understanding. With the following css : .bloc .field:nth-last-child(2){ ...some values... } and
I am having some troubles understanding the object relationships in Django. I wrote this:
I'm having some troubles understanding how Guice's singleton instantiations works. I've read the available
I'm having some troubles understanding MPL placeholders. Could someone please explain me why this
Having some trouble understanding the right approach here. I have a connection to a
I'm having some trouble understanding how codeigniters loading works. Well first you have the
I'm having some trouble understanding why the following doesn't result in a compiler error
I'm having some trouble understanding this issue. I have a class: class StringProperty {
I am having some troubles with understanding the circumstances under which Silverlight downloads a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.