In macOS 10.14 users can choose to adopt a system-wide light or dark appearance and I need to adjust some colours manually depend of the current mode.
While in Dark mode, the dock, menu bar, and all of your Apple apps, including Safari, Mail, Calendar, Notes, the Mac App Store, Messages, and more will feature darker colors and themes. Dark Mode will need to be built into third-party Mac apps that don't already offer a dark option when macOS Mojave is released.
To enable Dark Mode on a Mac, click the Apple logo in the top-left corner of your screen. Then select System Preferences and click the General button in the pop-up window. Finally, click Dark or Auto to enable Dark Mode. Click the Apple logo in the top-left corner of your Mac's screen.
Dark Mode was first introduced to Mac with OS X 10.10 Yosemite, and it has become a staple of all subsequent versions of macOS.
For that, you can return a different color based on the current interface style. Dark mode can be detected by using the userInterfaceStyle property on the current trait collection.
The feature Apple is promoting most heavily with macOS 10.14 Mojave is Dark mode, which the company advertises as “a dramatic new look that helps you focus on your work… as toolbars and menus recede into the background.” Let’s look at what Apple has done with Dark mode, after which you’ll have a better idea of what to think about while trying it.
Turn on Dark Mode. From the Apple menu , choose System Preferences. Click General. From the Appearance options at the top of the window, select Dark. This feature is available only after upgrading macOS Mojave.
Comparatively evident is another dark mode subject to Xcode, conceivably alluding to a more widespread “dull mode” on the Mac. Right now, neither iOS or macOS has a “genuine” dark mode. On the Mac, the menu banish can be transformed from a light to dark appearance, and on iOS, a feature known as Smart Invert can to some degree emulate the target.
If you turn on Dark Mode while using Dynamic Desktop, the desktop may change to the dark still image. You can change this setting in Desktop & Screen Saver preferences .
Since the actual appearance object you usually get via effectiveAppearance
is a composite appearance, asking for its name directly probably isn't a reliable solution.
Asking for the currentAppearance
usually isn't a good idea, either, as a view may be explicitly set to light mode or you want to know whether a view is light or dark outside of a drawRect:
where you might get incorrect results after a mode switch.
The solution I came up with looks like this:
BOOL appearanceIsDark(NSAppearance * appearance)
{
if (@available(macOS 10.14, *)) {
NSAppearanceName basicAppearance = [appearance bestMatchFromAppearancesWithNames:@[
NSAppearanceNameAqua,
NSAppearanceNameDarkAqua
]];
return [basicAppearance isEqualToString:NSAppearanceNameDarkAqua];
} else {
return NO;
}
}
You would use it like appearanceIsDark(someView.effectiveAppearance)
since the appearance of a specific view may be different than that of another view if you explicitly set someView.appearance
.
You could also create a category on NSAppearance
and add a - (BOOL)isDark
method to get someView.effectiveAppearance.isDark
(better chose a name that is unlikely to be used by Apple in the future, e.g. by adding a vendor prefix).
I have used the current appearance checking if the system is 10.14
+ (BOOL)isDarkMode {
NSAppearance *appearance = NSAppearance.currentAppearance;
if (@available(*, macOS 10.14)) {
return appearance.name == NSAppearanceNameDarkAqua;
}
return NO;
}
And to detect the change of mode in a view the methods are:
- (void)updateLayer;
- (void)drawRect:(NSRect)dirtyRect;
- (void)layout;
- (void)updateConstraints;
And to detect the change of mode in a view controller the methods are:
- (void)updateViewConstraints;
- (void)viewWillLayout;
- (void)viewDidLayout;
Using notification:
// Monitor menu/dock theme changes...
[NSDistributedNotificationCenter.defaultCenter addObserver:self selector:@selector(themeChanged:) name:@"AppleInterfaceThemeChangedNotification" object: nil];
-(void)themeChanged:(NSNotification *) notification {
NSLog (@"%@", notification);
}
For more information Dark Mode Documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With