How to Detect Switch Between MACos Default & Dark Mode Using Swift 3

How to detect switch between macOS default & dark mode using Swift 3

I'm using this Swift 3 syntax successfully:

DistributedNotificationCenter.default.addObserver(self, selector: #selector(interfaceModeChanged(sender:)), name: NSNotification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil)

func interfaceModeChanged(sender: NSNotification) {
...
}

Detecting iOS Dark Mode Change

Swift 5:

traitCollectionDidChange also gets called a few times. This is how I detect DarkMode runtime change and setColors().

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)

setColors()
}

In setColors() func I update the colors.
Detecting current colorScheme:

extension UIViewController {
var isDarkMode: Bool {
if #available(iOS 13.0, *) {
return self.traitCollection.userInterfaceStyle == .dark
}
else {
return false
}
}

}

I have colors defined like this (for iOS < 13):

enum ColorCompatibility {
static var myOlderiOSCompatibleColorName: UIColor {
if UIViewController().isDarkMode {
return UIColor(red: 33, green: 35, blue: 37, alpha: 0.85)
}
else {
return UIColor(hexString: "#F3F3F3", alpha: 0.85)
}
}
}

Example:

private func setColors() {
myView.backgroundColor = ColorCompatibility.myOlderiOSCompatibleColorName
}

Also you might need to call setColors in ViewDidLoad/Will/DidAppear depending on your case like this:

viewDidLoad() {
...
setColors()
...
}

For iOS11+ you could use "named Colors", defined in Assets and much easier to use in IB.

Cheers

Swift macOS | Setting (switching between) dark and lightmode

I was on the right track!

window.appearance = NSAppearance(named: .aqua) 

worked.

There is actually a guide to set the appearance of macOS apps from apple.

Note: This also works if you have all your content inside a NSPopover, since it too has a property appearance.

How to detect if OS X is in dark mode?

Don't think there's a cocoa way of detecting it yet, however you can use defaults read to check whether or not OSX is in dark mode.

defaults read -g AppleInterfaceStyle

Either returns Dark (dark mode) or returns domain pair does not exist.

EDIT:

As Ken Thomases said you can access .GlobalPreferences via NSUserDefaults, so

NSString *osxMode = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];

If osxMode is nil then it isn't in dark mode, but if osxMode is @"Dark" then it is in dark mode.

Turn on system dark mode from app - Swift


  • First question: Impossible at this moment
  • Second Question: Answer of Tamás Sengel

You should check the userInterfaceStyle variable of UITraitCollection, same as on tvOS and macOS.

switch traitCollection.userInterfaceStyle {
case .light: //light mode
case .dark: //dark mode
case .unspecified: //the user interface style is not specified
}

You should use the traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) function of UIView/UIViewController to detect changes in the interface environment (including changes in the user interface style).

From Apple Developer Documentation:

The system calls this method when the iOS interface environment changes. Implement this method in view controllers and views, according to your app’s needs, to respond to such changes. For example, you might adjust the layout of the subviews of a view controller when an iPhone is rotated from portrait to landscape orientation. The default implementation of this method is empty.

System default UI elements (such as UITabBar or UISearchBar) automatically adapt to the new user interface style.

How can I check whether dark mode is enabled in iOS/iPadOS?


SwiftUI

With the \.colorScheme key of an Environment variable:

struct ContentView: View {
@Environment(\.colorScheme) var colorScheme

var body: some View {
Text(colorScheme == .dark ? "In dark mode" : "In light mode")
}
}

Also, it automatically updates on the change of the environment color scheme.



UIKit

To check the current, all object those conform to UITraitEnvironment protocol, including all UIView subclasses and all UIViewConttroller subclasses have access to the current style:

myUIView.traitCollection.userInterfaceStyle == .dark
myUIViewController.traitCollection.userInterfaceStyle == .dark

To detect live changes of the style, here is the full detailed answer

How can dark mode be detected on macOS 10.14?

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).

Xcode / plist - run application always in Dark/Light theme on macOS

If you are looking for the full on SwiftUI version:

import SwiftUI

@main
struct SwiftUI_Football_GridApp: App {
var body: some Scene {
WindowGroup {
ContentView().preferredColorScheme(.light)
}
}
}


Related Topics



Leave a reply



Submit