NSPopUpButton,NSComboBox相似菜单 [英] NSPopUpButton, NSComboBox similar menu

查看:179
本文介绍了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.

推荐答案

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

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.

现在开始绘制.在您的NSComboBoxCell子类中,您需要覆盖 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView.

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

(我尝试覆盖- (void)drawInteriorWithFrame:(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.)

您的自定义- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView应该看起来像这样:

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
}

这篇关于NSPopUpButton,NSComboBox相似菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆