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 4064588
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:52:28+00:00 2026-05-20T15:52:28+00:00

I have a custom NSView which used to get created in a NIB and

  • 0

I have a custom NSView which used to get created in a NIB and assigned as the view for an NSMenuItem, which works great, but now I want to create the view in code (for good reason I can assure you) which did not look hard but but the view is not actually drawing.

The “drawRect:” message which was previously being called to draw the view when required is not being called anymore even when I send a “setNeedsDisplay:” message.

I init the view with an image and set the size (of the view to match the image size) which seems to work because the menu item is the right size, but there is no image.

What could be happening here?

This is the code to init the view:

-(id)initWithImage:(NSImage*)image
{
    self = [super init];

    if (self != nil)
    {
        self.imageToDisplay = image;

        // this bit does get called and resizes the view to match the image size
        NSRect imageBounds = NSMakeRect(0.0f, 0.0f, imageToDisplay.size.width, imageToDisplay.size.height);     
        [self setBounds:imageBounds];
        [self setFrame:imageBounds];

        [self setHidden:NO];
        [self setAlphaValue:1.0f];

        [self setAutoresizesSubviews:YES];
    }

    return self;
}

And this is the code for drawing the view which does not get called

// this is never called
-(void)drawRect:(NSRect)dirtyRect
{
    if (imageToDisplay == nil)
        return;

    NSRect imageBounds = NSMakeRect(0.0f, 0.0f, imageToDisplay.size.width, imageToDisplay.size.height);

    [self setBounds:imageBounds];
    [self setFrame:imageBounds];

    [imageToDisplay drawInRect:[self bounds]
                      fromRect:imageBounds
                     operation:NSCompositeSourceAtop
                      fraction:1.0f];
}

And this is the code for the menu item which adds the view.

-(void)awakeFromNib
{
    MyCustomView* view = [[MyCustomView alloc] init];

    [self setView:view];

    // i would have expected the image to get drawn at this point
    [view setNeedsDisplay:YES];
}
  • 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-05-20T15:52:29+00:00Added an answer on May 20, 2026 at 3:52 pm

    You have to set your view’s frame before you can set its bounds. In your -init..., either swap the two set... calls, or remove the setBounds: (bounds is set to {(0,0), (frame.size.width, frame.size.height)} by default anyways) and everything should work. I also don’t think that you need to set frame and bounds again in drawRect, and in fact it seems like a bad idea to change those when focus is already locked on your view; at best it will cause weird flashing if the values are actually different.

    UPDATE: just saw this note in the View Programming Guide:

    Note: Once an application explicitly sets the bounds rectangle of a view using any of the setBounds… methods, altering the view’s frame rectangle no longer automatically changes the bounds rectangle. See “Repositioning and Resizing Views” for details.

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

Sidebar

Related Questions

I have an NSStatusItem that has an NSMenuItem which contains a custom NSView. this
In my Cocoa Application I have created a Custom View Class (subclass of NSView),
I have a nib file. In this I created a new custom view (of
I have a custom NSView which contains a subview—for the purposes of solving this
I have a window which has a custom NSView and has a bottom bar
Here's the scenario - I have created a custom NSView subclass, and the implementation
I have a custom NSView (MyView), which is rendered by the NSViewCollectionItem. MyView has
I have an application where I have one custom view which is derived from
I've got a custom NSView subclass which I want to have rounded corners. I
I have a custom view class, which receives the mouse events. I do not

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.