本文介绍了NSMenuItem的视图(NSView子类的实例)在悬停时未突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用一个自定义的NSView子类来绘制某些内容,但是当用户将鼠标悬停时,它不会像突出显示的那样绘制,并且当用户单击它时,它不会关闭NSMenu.有什么想法吗?

I need to use a custom NSView subclass to draw some content, but it isn't drawing as highlighted when the user hovers and it doesn't dismiss the NSMenu when the user clicks on it. Any ideas?

因此,使用-drawRect:[[self enclosingMenuItem] isHighlighted]可以告诉我是否需要将视图绘制为突出显示,并有机会这样做.我需要弄清楚的是如何做到这一点.

So using -drawRect: and [[self enclosingMenuItem] isHighlighted] I'm able to tell whether or not I need to draw the view as highlighted and am given the chance to do so. All I have to figure out is how to do that.

推荐答案

也许您应该这样尝试:

#define menuItem ([self enclosingMenuItem])

- (void) drawRect: (NSRect) rect {
    BOOL isHighlighted = [menuItem isHighlighted];
    if (isHighlighted) {
        [[NSColor selectedMenuItemColor] set];
        [NSBezierPath fillRect:rect];
    } else {
        [super drawRect: rect];
    }
}

这篇关于NSMenuItem的视图(NSView子类的实例)在悬停时未突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:54