0

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

4

2 回答 2

4

要自定义 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
}
于 2012-01-20T23:59:08.197 回答
1

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

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

于 2013-03-22T10:40:34.823 回答