带有静态图像的弹出按钮(Cocoa OSX)
我正在尝试制作一个弹出按钮,该按钮始终显示 + 作为其图像,当您单击它时,会弹出一个上下文菜单,让您决定要添加的对象类型。有没有办法使用 NSPopupButton 来做到这一点?我在 NSPopupButotn 的规范中看到方法 SetImage 没有效果,因此看起来这可能无法使用此类。这是正确的吗?
I am trying to make a popup button that always displays a + as its image and when you click on it, a context menu pops up that will allow you to decide what type of object you want to add. Is there anyway to do this using an NSPopupButton? I saw in the specs for NSPopupButotn that the method SetImage has no effect so it seems that this is likely not going to work using this class. Is this correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
弹出按钮可以获取图像和信息。与其关联的菜单中第一个菜单项的标题。因此,最好将您希望在弹出按钮中显示的图像包含为第一个菜单项,然后隐藏其可见性。
如果你还不清楚,那么看一遍下面的视频,一定会让你明白。
本教程展示了如何在 Interface Builder 中创建带有 NSToolbar 下拉菜单的工具栏项 (NSToolbarItem):
链接
A popup button takes the Image & Title of the first menu-item in the menu associated with it. So its better to include the image, which you want to appear as you popup button, as first menu item and then hide its visibility.
If you aren't clear yet, then go through the following video once, it will surely make you understand.
This tutorial shows how to create a toolbar item (NSToolbarItem) with drop-down menu for NSToolbar in Interface Builder :
Link
为什么不直接使用 NSPopupButton 呢?使用 2 个按钮来执行一项任务似乎不太正确,而且如上所述,按住按钮时它的行为不正常。
要让弹出按钮按照您喜欢的方式工作...将其设置为下拉类型。不要给弹出按钮本身任何名称或图像。如果弹出按钮没有名称或图像,则第一个菜单项将成为按钮的标题。菜单项可以包含图像,因此不要给第一个菜单项任何标题,只需设置它的图像,这就是您将在弹出按钮上看到的图像。对于其余的菜单项,只需将它们正常添加到第一个菜单项之后即可。请注意,如果您以编程方式更改代码中的菜单项,只需确保保持第一个菜单项不变,一切都会正常。我在一些应用程序中这样做没有任何问题。
Why not just use an NSPopupButton? Using 2 buttons to perform one task doesn't seem right plus as mentioned it doesn't behave in the normal manner regarding holding on the button.
To get the popup button working how you like... set it to be a pull-down type. Don't give the popup button itself any name or image. If the popup button doesn't have a name or image then the first menu item becomes the title of the button. Menu items can have images so don't give the first menu item any title, just set it's image and that's the image you'll see on the popup button. For the rest of the menu items, just add them as normal after the first menu item. Note that if you programmatically change the menu items in code, just make sure to leave the first menu item in tact and everything will be OK. I do this in a few of my applications with no problem.
关键是使用
NSPopUpButtonCell的
setUsesItemFromMenu
:。 Apple 文档包含如何使用它的示例。The key is to use
NSPopUpButtonCell's
setUsesItemFromMenu
:. The Apple docs include an example of how to use this.是的,
NSPopUpButton
的可见外观不符合您的要求。您想要的是一个带有附加菜单的常规
NSButton
。在 Interface Builder 中清除标题,将 Bezel 设置为 Square,将 Position 设置为仅中心图标版本,将图像设置为NSAddTemplate
。然后创建一个 NSPopUpButton ,将其设置为“下拉”,隐藏,并将其对齐/设置其大小到 NSButton 的底部。
最后,将
NSButton
中的performClick:
连接到NSPopUpButton
上的performClick:
。这不会处理按住按钮的情况;为此,您必须编写一些代码来使用(非绘图)
NSPopUpButtonCell
。Yes, the visible appearance of
NSPopUpButton
is wrong for what you want.What you want is a regular
NSButton
with a menu attached. In Interface Builder clear the title, set the Bezel to Square, the Position to the center icon-only version and the image toNSAddTemplate
.Then create a
NSPopUpButton
, set it to "Pull Down", hidden, and align it/set its size to the bottom of the NSButton.Finally, connect
performClick:
in theNSButton
toperformClick:
on theNSPopUpButton
.That won't handle a click-hold on the button; for that you'll have to write some code to use a (non-drawing)
NSPopUpButtonCell
.