我创建了一个简单的NSStatusBar
,其中NSMenu
设置为菜单。我也为这个菜单添加了一些NSMenuItems
,这些菜单工作正常(包括选择器和高亮显示),但只要添加了自定义视图(setView :),就不会出现高亮显示。使用自定义视图突出显示NSMenuItem?
CustomMenuItem *menuItem = [[CustomMenuItem alloc] initWithTitle:@"" action:@selector(openPreferences:) keyEquivalent:@""];
[menuItem foo];
[menuItem setTarget:self];
[statusMenu insertItem:menuItem atIndex:0];
[menuItem release];
而且我foo的方法是:
- (void)foo {
NSView *view = [[NSView alloc] initWithFrame:CGRectMake(5, 10, 100, 20)];
[self setView:view];
}
如果我删除的setView方法,它会突出。
我已搜索和搜索,无法找到实现/启用此方法。
编辑
我按照此问题的代码在我的NSView子类实现亮点:
An NSMenuItem's view (instance of an NSView subclass) isn't highlighting on hover
#define menuItem ([self enclosingMenuItem])
- (void) drawRect: (NSRect) rect {
BOOL isHighlighted = [menuItem isHighlighted];
if (isHighlighted) {
[[NSColor selectedMenuItemColor] set];
[NSBezierPath fillRect:rect];
} else {
[super drawRect: rect];
}
}
这是一个无用的'#define'。 – NSAddict 2014-02-04 12:21:38
[NSMenuItem的视图(NSView子类的实例)在悬停时不突出显示]的可能重复(http://stackoverflow.com/questions/2917713/an-nsmenuitems-view-instance-of-an-nsview-subclass -isnt-highlighting-on-hove) – nschum 2014-10-25 13:53:16