我創建了一個簡單的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