使用自定义视图突出显示 NSMenuItem?
我创建了一个简单的 NSStatusBar
,并将 NSMenu
设置为菜单。我还在这个菜单中添加了一些 NSMenuItems
,它们工作正常(包括选择器和突出显示),但是一旦我添加自定义视图(setView:),就不会发生突出显示。
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 子类中遵循此问题中的代码来实现突出显示:
NSMenuItem 的视图(NSView 子类的实例)在悬停时不突出显示
#define menuItem ([self enclosingMenuItem])
- (void) drawRect: (NSRect) rect {
BOOL isHighlighted = [menuItem isHighlighted];
if (isHighlighted) {
[[NSColor selectedMenuItemColor] set];
[NSBezierPath fillRect:rect];
} else {
[super drawRect: rect];
}
}
I have created a simple NSStatusBar
with a NSMenu
set as the menu. I have also added a few NSMenuItems
to this menu, which work fine (including selectors and highlighting) but as soon as I add a custom view (setView:) no highlighting occurs.
CustomMenuItem *menuItem = [[CustomMenuItem alloc] initWithTitle:@"" action:@selector(openPreferences:) keyEquivalent:@""];
[menuItem foo];
[menuItem setTarget:self];
[statusMenu insertItem:menuItem atIndex:0];
[menuItem release];
And my foo method is:
- (void)foo {
NSView *view = [[NSView alloc] initWithFrame:CGRectMake(5, 10, 100, 20)];
[self setView:view];
}
If I remove the setView method, it will highlight.
I have searched and searched and cannot find a way of implementing/enabling this.
Edit
I implemented highlight by following the code in this question in my NSView SubClass:
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];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是上述内容的一个不太冗长的版本。这对我来说效果很好。 (backgroundColour 是一个 ivar。)
Here's a rather less long-winded version of the above. It's worked well for me. (backgroundColour is an ivar.)
2019 年更新:
将其中之一设置为您的
menuItem.view
。(这要归功于 Sam Soffes,他帮助我解决了这个问题,并将几乎该代码一字不差地发给了我。)
Update for 2019:
Set one of those to your
menuItem.view
.(Credit belongs to Sam Soffes who helped me figure this out and sent me almost that code verbatim.)
如果您要向菜单项添加视图,则该视图必须自行绘制突出显示。恐怕你不能免费得到它。从 菜单编程主题:
If you're adding a view to a menu item, that view has to draw the highlight itself. You don't get that for free, I'm afraid. From the Menu Programming Topics:
是的,正如前面提到的,你必须自己绘制。我使用 AppKit 的 NSDrawThreePartImage(…) 进行绘制,还包括检查以使用用户的控件外观(蓝色或石墨)。为了获取图像,我只是从屏幕截图中获取它们(如果有人知道更好的方法,请添加评论) .)这是我的MenuItemView的drawRect的一部分:
编辑
应该定义这些:
这些对应于系统偏好设置中的两个外观选项。另外,menuItemFillGray & menuItemFillBlue 只是标准菜单项填充渐变的 NSImage。
Yes, as mentioned earlier you must draw it yourself. I use AppKit's NSDrawThreePartImage(…) to draw, and also include checks to use the user's control appearance (blue or graphite.) To get the images, I just took them from a screenshot (if anyone knows a better way, please add a comment.) Here's a piece of my MenuItemView's drawRect:
EDIT
Should have defined these:
These correspond to the two appearance options in System Preferences. Also, menuItemFillGray & menuItemFillBlue are just NSImages of the standard menu item fill gradients.