Trouble matching the vibrant background of a Yosemite NSMenuItem containing a custom view

前端 未结 7 1270
一向
一向 2020-12-14 16:19

I am attempting to add a custom view to an NSMenuItem in the OS X 10.10 Yosemite menu bar.

The custom view is simply an NSView background with an NSTextField “label”

7条回答
  •  时光取名叫无心
    2020-12-14 17:10

    Response from a Apple Developer Technical Support ticket I opened in 2015:

    Re: DTS Auto-Ack - Vibrant background and highlighting of Custom View NSMenuItems

    This is a difficult problem to tackle, especially in light of the fact that menu selection drawing was not intended for menu items with custom views, and menu selection drawing (colors, etc.) may change in the future. This is why we ask you to file bug reports so that menu selection will be honored with custom views, if asked for, so that future changes to OS X won’t require developers to continually maintain their code to match future color appearances.

    The “Application Menu and Pop-up List Programming Topics” says this:

    Views in Menu Items -

    “A menu item with a view does not draw its title, state, font, or other standard drawing attributes, and assigns drawing responsibility entirely to the view. Keyboard equivalents and type-select continue to use the key equivalent and title as normal.”

    Since all drawing is up to the developer, custom views in menu items aren’t necessarily supposed to draw “selected”.

    The APIs to obtain the right selection color is obviously not doing what it’s supposed to, hence the request to file a bug report. I wish we could offer more concrete solutions to the problem but a workaround offered today may not hold up tomorrow and we don’t want to set a bad precedent on workarounds that are risky. Apple apps have access to lower level private APIs that achieve their results. We cannot offer you these solutions as they are private.

    If selectedMenuItemColor() does not match the menu highlight color with Vibrant light and dark, that’s a bug to be filed and to be fixed.

    Lastly, Apple recommends to use NSMenuItem’s APIs as much as possible to achieve what you want in menus. The screenshots you included can likely be done without applying custom views.

提交回复
热议问题