NSPopUpButton、NSComboBox 类似菜单

发布于 2024-12-03 05:17:59 字数 184 浏览 0 评论 0原文

我正在尝试创建一个带有下拉菜单的菜单,每个单元格都有自定义背景。 首先,我尝试改编 NSPopUpButton 但我找不到更改单元格背景图像的方法。使用 setImage: 会将文本滑动到背景的右侧。接下来我停在 NSComboBox 但我找不到更改箭头按钮的方法。有人可以提供帮助和想法吗?接下来的事情是创建一个自定义控制器,但我想使用已经完成的东西。

I'm trying to create a menu with a drop down menu with a custom background for every cell.
First i tried to adapt NSPopUpButton but i couldn't find a way to change the cells background image. Using setImage: would slide the text to the right of the background. Next I stopped at NSComboBox but i couldn't find a way to change the arrow button. Can someone please help with and idea? Next thing would be to create a custom controller but i would like to use something already done.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

月亮邮递员 2024-12-10 05:17:59

要自定义 NSComboBox 中的箭头按钮,您需要创建 NSComboBoxCell 的子类并将组合框设置为使用该单元格。如果您已在 IB 中配置了控件,则可以轻松更改单元格的类别。如果您以编程方式创建组合框,请创建 NSComboBox 的子类,覆盖 + (Class)cellClass 并从该方法返回自定义 NSComboBoxCell 子类。

现在开始绘图。在你的 NSComboBoxCell 子类中,你需要重写
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView

(我尝试覆盖 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView 但它提供的单元格框架无法绘制实际的按钮区域,即它只覆盖了文本输入区域。)

您的自定义 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView 应该如下所示:

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    [super drawWithFrame:cellFrame inView:controlView];

    // Constrain to the far right of the provided frame to draw the button
    NSRect bounds = NSMakeRect(cellFrame.origin.x + cellFrame.size.width - cellFrame.size.height, cellFrame.origin.y, cellFrame.size.height, cellFrame.size.height);

    // Draw your custom button inside the bounds rect
}

To customise the arrow button in NSComboBox, you need to create a subclass of NSComboBoxCell and set your combo box to use that cell. If you've configured your control in IB, you can easily change the class of your cell there. If you're programmatically creating your combo box, create a subclass of NSComboBox, override + (Class)cellClass and return your custom NSComboBoxCell subclass from that method.

Now for the drawing. In your NSComboBoxCell subclass, you need to override
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView.

(I've tried overriding - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView but the cell frame it provides stops short of drawing the actual button area, i.e. it only covers the text input area.)

Your custom - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView should look something like this:

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    [super drawWithFrame:cellFrame inView:controlView];

    // Constrain to the far right of the provided frame to draw the button
    NSRect bounds = NSMakeRect(cellFrame.origin.x + cellFrame.size.width - cellFrame.size.height, cellFrame.origin.y, cellFrame.size.height, cellFrame.size.height);

    // Draw your custom button inside the bounds rect
}
情愿 2024-12-10 05:17:59

我不确定我是否正确理解了你的问题。如果您想在 UI 中的任意位置显示菜单: NSMenu 提供了实现此目的的便捷方法。查看 + popUpContextMenu:withEvent:forView:+ popUpContextMenu:withEvent:forView:withFont:– popUpMenuPositioningItem:atLocation:inView:< 的文档/code> 找到最适合您需求的一个。这样您就可以在您喜欢的任何位置显示菜单。

如果您想在菜单中显示任意内容,请查看 NSMenuItem- setView: 的文档。这允许您在菜单内插入视图。结合上述在任意位置显示菜单的方法,您可以创建满足“PopOver”需求的各种解决方案。

I'm not sure if I understood your question correctly. If you want to show a menu at an arbitrary position somewhere in your UI: NSMenu provides convenience methods for achieving that. Have a look at the documentation for + popUpContextMenu:withEvent:forView:, + popUpContextMenu:withEvent:forView:withFont: and – popUpMenuPositioningItem:atLocation:inView: to find the one that best fits your needs. Like that you can display a menu at whatever position you like.

If you instead want to display arbitrary content inside a menu, have a look at the documentation of NSMenuItem's - setView:. This allows you to insert views inside menus. Together with the above method of displaying menus wherever you want, you can create all sorts of solutions for "PopOver" needs.

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