简体   繁体   中英

How to get dynamic NSMenu to use Mojave Dark Mode?

My OS is set to dark mode, and my whole app renders properly, except for some dynamically created NSMenu instances, which render in the old light style.

How do I get those menus to render using a dark visual style?

The dark visual style is only applied if you specify the parent view.

If view is nil here, the old style will be used instead:

NSMenu* menu;
NSView* view; // cannot be nil
[menu popUpMenuPositioningItem:nil atLocation:NSMakePoint(0, 0) inView:view];

There is an undocumented apperance argument for popUpMenuPositioningItem . You have to customize the existing NSMenu class to use it:

@interface NSMenu ()
- (BOOL)popUpMenuPositioningItem:(nullable NSMenuItem *)item atLocation:(NSPoint)location inView:(nullable NSView *)view appearance:(nullable NSAppearance *)appearance NS_AVAILABLE_MAC(10_6);
@end

If you want to use it in Swift via popUp you have to declare it in an "Objective-C Bridging Header" file and add the path to the Xcode project's build settings under "Swift Compiler - General"

I found this solution here and here . Class extension is documented here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM