How to set dark mode for NSToolbar in macOS Catalyst app? - ios

In my iOS/macOS app the user can choose between a light and a dark appearance.
This is done by setting the overrideUserInterfaceStyle of the app window, for example
window.overrideUserInterfaceStyle = .dark
It works fine on iOS/iPadOS.
On macOS (Catalyst) the app uses an NSToolbar, which always appears in the mode chosen in the system settings, regardless what ist set for the overrideUserInterfaceStyle of the window (see screenshot).
Since this is not a good visual impression, I would like to know how I can set light/dark mode for the NSToolbar too.

Add UIUserInterfaceStyle with a value Dark to your app’s Info.plist. That will force the whole app to dark mode on iOS 13+ and Mac Catalyst (you won’t need to set overrideUserInterfaceStyle).
Docs: https://developer.apple.com/documentation/bundleresources/information_property_list/uiuserinterfacestyle

This can be done by adding an macOS plugin which has access to the whole AppKit API. Instructions can be found here:
How to Access the AppKit API from Mac Catalyst Apps
Then you can set the desired appearance there at runtime, for example:
NSApplication.shared.appearance = NSAppearance(named: .darkAqua)

Related

iOS, Swift Application is always running in Dark Mode

So I just want my app to always run in dark mode. Even if the device appearance in in Light I still want the app to run in dark mode. Is this possible?
Yes, It's possible, You must Add "Appearance" key to "Dark" in Info.plist such as:
You can set UIUserInterfaceStyle to Dark in your Info.plist as explained in Is it possible to opt-out of dark mode on iOS 13?

Launched iOS simulator from Xcode 11.3 and getting a black screen

I was using Xcode 10. Did not have enough space to update to latest Xcode. So downloaded it from Apple's website and removed the old Xcode. To make more space I deleted all the cache, unavailable simulators, derived data and unwanted stuffs following this. After that installed the new Xcode 11.3.
My app is build successfully. But the emulator is showing only black screen.
I tried
"Erase All Content and Settings"
and also
defaults write com.apple.CoreSimulator.IndigoFramebufferServices
FramebufferRendererHint 3
But still emulator is the same.
Switching from Xcode 10 to Xcode 11 enables the dark mode/Dynamic Colors on your project.
There are two possible scenarios for your problem,
The Simulator may be on the dark mode and your first view controller background automatically changes to dark mode.
The Simulator may be on Light mode, but you have set your first view controller to be in a dark colour
For the first scenario,
Go to Settings in Emulator -> scroll all the way down to Developer -> Switch off the dark appearance.
If you want to avoid using the interface Styles/ Light mode or dark modes, add the following in your info.plist file if you're opening as XML,
<key>UIUserInterfaceStyle</key>
<string>Light</string>
if you're using the plist file,
use the key,
UIUserInterfaceStyle
and set the String value as Light.
If you think you're facing the second scenario,
Change the dynamic system background colour of your view to a hardcoded UIColor in Interface builder or your ViewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
yourView.backgroundColor = UIColor.white
}

iOS 13 dark mode objc strings

I want to disable dark mode within my app for iOS 13. And I prefer not to do so via info.plist. The reason is, plist uses a global setting, making UIUserInterfaceStyle affects earlier iOS versions aswell (creating conflicts). I only want to set it to light for iOS 13! This is my current code within my AppDelegate:
if(#available(iOS 13, *)){
window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}
This code snippet works for fixing statusBar color to light. But general strings within the app are still in "dark mode". Earlier white strings turn into black, and my app is already pretty dark as it is. How do I fix so strings in the app does not change it's colors by themselves in dark mode? I want a global code snippet and not go through every ViewController...
Regards
iOS ignores Info.plist keys it doesn't know. So you can safely set UIUserInterfaceStyle to Light in your Info.plist, iOS prior to version 13 will ignore this key.
Fixed the problem by calling:
self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;

SKStoreKitReviewController and Darkmode

Is there any way to alter the colors/tint of an SKStoreKitReviewController? When I am modifying my apps to support DarkMode, when in dark mode, the presented viewController does not look good.
This issue was resolved in iOS 13.2. The blur effect is dark, just like other system alerts when Dark Mode is enabled.

NSScrollView in a popup became transparent under MacOS Mojave

In a view displayed as a Popover, I implement a help subview (a NSView with a NSScrollView as a subview) that appears when user click on a button. With Sierra & High Sierra, everything was all right, but since I upgraded the dev MacPro to Mojave, the help bubble became slightly transparent. Elements (NSTextField or NSButton) being the help view are still visible as a white strip in Dark Mode (or as a gray strip in Light Mode)
I have defined the help view and its scroll view to have their own layer and these two layers are set as opaque
[help setWantsLayer:YES];
help.layer.opaque = TRUE;
help.backgroundColor = [NSColor colorWithRed:1. green:1. blue:140./255. alpha:1.];
[help.scrollView setWantsLayer:YES];
help.scrollView.layer.opaque = TRUE;
[help.scrollView setBackgroundColor:[NSColor colorWithRed:1. green:1. blue:140./255. alpha:1.]];
All background colours are set to this yellow tone.
This problem occurs only if the view that display the help view was presented as a NSPopover, not as a NSView from an ordinary window. Any idea?
add NSRequiresAquaSystemAppearance = YES in your info.plist
To run any app in Dark Mode, you must to be running macOS Mojave 10.14 or greater. By default, all existing apps will run in Light Mode even if the system is configured to run in Dark Mode. An app that is launched on macOS Mojave will run in Dark Mode when two criteria are met:
The system considers the app to be compatible with Dark Mode
The running application’s appearance is set to Dark Aqua
An app’s compatibility with Dark Mode is determined by a combination of the SDK it was built against, and the value of the “NSRequiresAquaSystemAppearance” Info.plist key. If your app is built against the 10.14 SDK or later, it will be considered compatible unless the key is set to YES. If your app is built against the 10.13 SDK or earlier, it is considered incompatible unless the Info.plist key is set to NO.

Resources