Cocoa - NSMenuItem 中的自定义 NSView 将不会绘制

发布于 2024-10-19 23:30:41 字数 1603 浏览 2 评论 0 原文

我有一个自定义 NSView,它曾经在 NIB 中创建并分配为 NSMenuItem 的视图,效果很好,但现在我想在代码中创建视图(有充分的理由我可以向你保证),这看起来并不难但视图实际上并不是在绘制。

即使我发送“setNeedsDisplay:”消息,之前在需要时调用以绘制视图的“drawRect:”消息也不再被调用。

我用图像初始化视图并设置视图的大小(视图的大小以匹配图像大小),这似乎有效,因为菜单项的大小正确,但没有图像。

这里可能发生什么?

这是初始化视图的代码:

-(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;
}

这是用于绘制不会被调用的视图的代码

// 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];
}

这是添加视图的菜单项的代码。

-(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];
}

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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

银河中√捞星星 2024-10-26 23:30:41

您必须先设置视图的frame,然后才能设置其bounds。在您的 -init... 中,交换两个 set... 调用,或者删除 setBounds: (bounds< /code> 默认情况下设置为 {(0,0), (frame.size.width, frame.size.height)} )并且一切都应该正常。我也不认为你需要在 drawRect 中再次设置 framebounds ,事实上,改变它似乎是一个坏主意当焦点已经锁定在您的视图上时;如果值实际上不同,充其量只会导致奇怪的闪烁。

更新:刚刚在 查看编程指南

注意:一旦应用程序使用任何 setBounds... 方法显式设置视图的边界矩形,更改视图的框架矩形将不再自动更改边界矩形。请参阅“重新定位和调整视图大小”了解详细信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文