NSPopUpButton、NSComboBox 类似菜单
我正在尝试创建一个带有下拉菜单的菜单,每个单元格都有自定义背景。 首先,我尝试改编 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要自定义 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
应该如下所示: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:我不确定我是否正确理解了你的问题。如果您想在 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.