NSPopUpButton, NSComboBox similar menu

前端 未结 2 1416
萌比男神i
萌比男神i 2021-01-26 05:14

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 b

2条回答
  •  臣服心动
    2021-01-26 06:11

    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
    }
    

提交回复
热议问题