macOS 获取系统设置外观获取系统深色模式皮肤
#import "ViewController.h" - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //控件布局 [self layoutSubViews]; // 判断的方式 NSDictionary *dict = [[NSUserDefaults standardUserDefaults] persistentDomainForName:NSGlobalDomain]; id style = [dict objectForKey:@"AppleInterfaceStyle"]; BOOL isDarkMode = ( style && [style isKindOfClass:[NSString class]] && NSOrderedSame == [style caseInsensitiveCompare:@"dark"] ); if (isDarkMode) { NSLog(@"777黑夜模式"); } else { NSLog(@"777正常模式"); } }
还有动态监测
#import "ViewController.h" - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //控件布局 [self layoutSubViews]; // 动态监听 Mode 的修改 NSString * const darkModeNotificationName = @"AppleInterfaceThemeChangedNotification"; [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDarkMode) name:darkModeNotificationName object:nil]; } // 监听的修改方法 - (void)updateDarkMode { NSDictionary *dict = [[NSUserDefaults standardUserDefaults] persistentDomainForName:NSGlobalDomain]; id style = [dict objectForKey:@"AppleInterfaceStyle"]; BOOL isDarkMode = ( style && [style isKindOfClass:[NSString class]] && NSOrderedSame == [style caseInsensitiveCompare:@"dark"] ); if (isDarkMode) { NSLog(@"77777黑夜模式"); } else { NSLog(@"77777正常模式"); } }