2011-09-06 150 views
0

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

回答

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 
} 
1

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

如果您不想在菜单中显示任意内容,请查看NSMenuItem的文档- setView:。这使您可以在菜单中插入视图。再加上上面显示菜单的方法,您可以为“PopOver”需求创建各种解决方案。

相关问题