【问题标题】:NSPopUpButton, NSComboBox similar menuNSPopUpButton、NSComboBox 类似的菜单
【发布时间】:2013-03-22 18:40:34
【问题描述】:

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

【问题讨论】:

    标签: nspopupbutton nscombobox


    【解决方案1】:

    要自定义 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
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多