NSPopUpButton, NSComboBox similar menu

Deadly 提交于 2019-12-02 10:13:29

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:

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!