Highlighting a NSMenuItem with a custom view?

前端 未结 4 1685
抹茶落季
抹茶落季 2020-12-25 14:08

I have created a simple NSStatusBar with a NSMenu set as the menu. I have also added a few NSMenuItems to this menu, which work fine (

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-25 15:02

    Here's a rather less long-winded version of the above. It's worked well for me. (backgroundColour is an ivar.)

    - (void)drawRect:(NSRect)rect
    {
        if ([[self enclosingMenuItem] isHighlighted]) {
            [[NSColor selectedMenuItemColor] set];
        } else if (backgroundColour) {
            [backgroundColour set];
        }
        NSRectFill(rect);
    }
    

提交回复
热议问题