如何创建一个使用固定图像且没有箭头的 NSPopUpButton?
我试图让 NSPopUpButton
像标准 NSButton
一样仅使用图像集进行渲染,但没有任何运气。
与 Mail.app 中的“+”按钮非常相似:
我认为他们是用 NSPopUpButton
做到的。我尝试过的显而易见的事情是:
NSMenuItem *imageItem = [[[NSMenuItem alloc] init] autorelease];
[imageItem setImage:[NSImage imageNamed:NSImageNameAddTemplate]];
[[popUpButton cell] setUsesItemFromMenu:NO];
[[popUpButton cell] setMenuItem:imageItem];
[[popUpButton cell] setImagePosition:NSImageOnly];
但这并不显示图像,而是仅显示一对箭头(我怀疑它们是在图像所在的位置绘制的)。调用 [popUpButton setImage:...]
也不会执行任何操作。
是否有记录的方法可以做到这一点,或者它是否归结为某些自定义子类化?
I'm trying to get NSPopUpButton
to render like a standard NSButton
with only an image set, but not having any luck.
Much like the "+" button in Mail.app:
I assume they did this with NSPopUpButton
. The obvious thing I've tried is:
NSMenuItem *imageItem = [[[NSMenuItem alloc] init] autorelease];
[imageItem setImage:[NSImage imageNamed:NSImageNameAddTemplate]];
[[popUpButton cell] setUsesItemFromMenu:NO];
[[popUpButton cell] setMenuItem:imageItem];
[[popUpButton cell] setImagePosition:NSImageOnly];
This doesn't show the image however, instead it just shows a pair of arrows (I suspect they're drawn over where the image would be). Calling [popUpButton setImage:...]
also does nothing.
Is there a documented way to do this, or does it come down to some custom subclassing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要防止按钮显示箭头:
To prevent the button from displaying the arrow:
这是我最终在 Swift 中使用的代码(模仿 Apple 的 菜单疯狂演示应用程序)。
确保按钮的宽度/高度对于图像来说足够大。我发现按钮右侧有一些填充覆盖了我的小图像,直到我使按钮足够宽以容纳神秘的填充。
Here is the code I used to finally get this working in Swift (modeled after Apple's Menu Madness demo app).
Be sure to make the width/height of the button big enough for the image. I found there was some padding on the right side of the button that covered my small image until I made the button wide enough to accommodate the mysterious padding.
在您的示例中,是的,它可能是使用
NSPopUpButton
实现的,但您真正想要的是一个将-pullsDown:
设置为 <代码>是。这是在 Interface Builder 中最容易设置的。更简单的是,使用 BWToolkit,它具有按钮栏和专门用于此目的的自定义按钮。
In your example, yes it is probably implemented with an
NSPopUpButton
, but rather than trying to customize the cell, what you really want is a button with-pullsDown:
set toYES
.This is easiest to set up in Interface Builder. Even easier, use BWToolkit which features a button bar and custom buttons specifically for this purpose.
@Laurent Etiemble(现已删除)的回答帮助了我和OP。
@Laurent Etiemble's (now deleted) answer helped me and the OP.
在 Xcode 4.4 中,您可以使用 Interface Builder 来完成所有这些操作。
NSPopUpButton
拖到您的窗口中,NSImage
您想要在按钮顶部放置的任何图标。我发现使用
NSImage
而不是设置按钮的图像效果更好。设置按钮图像会导致在弹出菜单中选择项目时出现问题。In Xcode 4.4 you can do all this by using Interface Builder.
NSPopUpButton
to your window,NSImage
of whatever icon you want on top of the button.I found that using the
NSImage
instead of setting the image of the button worked much better. Setting the button image caused problems when selecting items in the popup menu.