如何创建一个使用固定图像且没有箭头的 NSPopUpButton?

发布于 2024-10-06 06:55:59 字数 749 浏览 6 评论 0原文

我试图让 NSPopUpButton 像标准 NSButton 一样仅使用图像集进行渲染,但没有任何运气。

与 Mail.app 中的“+”按钮非常相似:

Not clicked Clicked

我认为他们是用 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:

Not clicked
Clicked

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情场扛把子 2024-10-13 06:55:59

要防止按钮显示箭头:

[popupButton.cell setArrowPosition:NSPopUpNoArrow];

To prevent the button from displaying the arrow:

[popupButton.cell setArrowPosition:NSPopUpNoArrow];
岁月静好 2024-10-13 06:55:59

这是我最终在 Swift 中使用的代码(模仿 Apple 的 菜单疯狂演示应用程序)。

func setUpButton() {
    let popupButton = NSPopUpButton()
    popupButton.translatesAutoresizingMaskIntoConstraints = false
    popupButton.pullsDown = true
    popupButton.bordered = false
    let popupCell = popupButton.cell as! NSPopUpButtonCell
    popupCell.arrowPosition = NSPopUpArrowPosition.NoArrow
    popupButton.addItemWithTitle("")
    var item = popupButton.itemAtIndex(0)
    item?.image = NSImage(named: "add")
    item?.onStateImage = nil
    item?.mixedStateImage = nil
    popupButton.addItemWithTitle("Item1")
    item = popupButton.itemAtIndex(1)
    item?.action = #selector(item1Pressed)
    item?.target = self
    popupButton.addItemWithTitle("Item2")
    item = popupButton.itemAtIndex(2)
    item?.action = #selector(item2Tapped)
    item?.target = self
    self.addSubview(popupButton)

    // NOTE: These are auto layout helper methods I made that you won't have. 
    // Just note that here is where I set up the auto layout constraints.
    popupButton.widthConstraint(40)
    popupButton.heightConstraint(20)
    popupButton.centerVerticallyInSuperview()
    popupButton.trailingSpaceToSuperview(0)
}

// TO MAKE SURE ALL ITEMS ALWAYS VALID (WHAT I WANTED FOR MY USE CASE)
override func validateMenuItem(menuItem: NSMenuItem) -> Bool {
    return true
}

func item1Pressed() {
    // do stuff
}

func item2Pressed() {
    // do stuff
}

确保按钮的宽度/高度对于图像来说足够大。我发现按钮右侧有一些填充覆盖了我的小图像,直到我使按钮足够宽以容纳神秘的填充。

Here is the code I used to finally get this working in Swift (modeled after Apple's Menu Madness demo app).

func setUpButton() {
    let popupButton = NSPopUpButton()
    popupButton.translatesAutoresizingMaskIntoConstraints = false
    popupButton.pullsDown = true
    popupButton.bordered = false
    let popupCell = popupButton.cell as! NSPopUpButtonCell
    popupCell.arrowPosition = NSPopUpArrowPosition.NoArrow
    popupButton.addItemWithTitle("")
    var item = popupButton.itemAtIndex(0)
    item?.image = NSImage(named: "add")
    item?.onStateImage = nil
    item?.mixedStateImage = nil
    popupButton.addItemWithTitle("Item1")
    item = popupButton.itemAtIndex(1)
    item?.action = #selector(item1Pressed)
    item?.target = self
    popupButton.addItemWithTitle("Item2")
    item = popupButton.itemAtIndex(2)
    item?.action = #selector(item2Tapped)
    item?.target = self
    self.addSubview(popupButton)

    // NOTE: These are auto layout helper methods I made that you won't have. 
    // Just note that here is where I set up the auto layout constraints.
    popupButton.widthConstraint(40)
    popupButton.heightConstraint(20)
    popupButton.centerVerticallyInSuperview()
    popupButton.trailingSpaceToSuperview(0)
}

// TO MAKE SURE ALL ITEMS ALWAYS VALID (WHAT I WANTED FOR MY USE CASE)
override func validateMenuItem(menuItem: NSMenuItem) -> Bool {
    return true
}

func item1Pressed() {
    // do stuff
}

func item2Pressed() {
    // do stuff
}

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.

束缚m 2024-10-13 06:55:59

在您的示例中,是的,它可能是使用 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 to YES.

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.

╭⌒浅淡时光〆 2024-10-13 06:55:59

@Laurent Etiemble(现已删除)的回答帮助了我和OP。

看看 此线程和Apple的示例菜单
Madness
:它们包含一些示例代码,用于将图像放在
NSPopUpButton

@Laurent Etiemble's (now deleted) answer helped me and the OP.

Take a look at this thread and the Apple's sample Menu
Madness
: they contains some sample code to put an image on a
NSPopUpButton.

奶气 2024-10-13 06:55:59

在 Xcode 4.4 中,您可以使用 Interface Builder 来完成所有这些操作。

  1. 将标准的 NSPopUpButton 拖到您的窗口中,
  2. 选择样式作为您想要的任何类型的按钮,
  3. 选择具有图像的按钮但不设置图像,
  4. 拖动 NSImage 您想要在按钮顶部放置的任何图标。

我发现使用 NSImage 而不是设置按钮的图像效果更好。设置按钮图像会导致在弹出菜单中选择项目时出现问题。

In Xcode 4.4 you can do all this by using Interface Builder.

  1. drag the standard NSPopUpButton to your window,
  2. select the Style as whatever type of button you want,
  3. chose the button to have an image but don't set the image,
  4. drag an 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文