Dark mode don't update instantly - ios

I have implemented dark mode in my iOS app, and while it works, the changes don't occur immediately. I think this has to do with the views not recreating themselves all the time like they do in android.
For instance, it doesn't work to go out from the app, then to settings and change theme, then back in the app again. The changes have only then occurred to Apple-related framework like alertControllers. To get dark mode to work I have to click around to a new view, then go back to the old view again.
I have tried adding dark mode code in the delegate, as I thought this would run every time you reenter your app, but it didn't.
So, any ideas on how to make this work? Is there for instance a delegate that is run every time I reenter the app, or can I have an observer to dark mode changes?

You could implement this method to "listen" to TraitCollection changes:
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
// do whatever you want to do
}
Also, as Gergely mentioned, try to use xcassets for colors and images so they dynamically change depending on the current TraitCollection.

Alert view controllers update because they use responsive system colors for their text and background, which have versions for both light and dark mode by default and update automatically once the mode changes.
You can do the same with custom views if you use color and image assets. this is a nice overview of how to do things: Medium Article.
The official Apple documentation also has a nice overview of how to support dark mode: Supporting Dark Mode in Your Interface.

Related

Force ViewController light mode but keep WKWebView mode adaptable

I'm using the newest version of Xcode for my iOS app.
I'm stuck with one small task since hours:
I want to force my View Controller to always stay in light mode, also if device is in dark mode. I'm using this code:
overrideUserInterfaceStyle = .light
This is working very well.
But there's a WKWebView in the View Controller. I want this WKWebView to stay adaptable to the color mode, so I can check via CSS if the device is in light mode or dark mode.
I tried so much but nothing worked.
Is it even possible? How?
I suggest changing UIInterfaceStyle just right before invoking WKWebView and setting it back before WKWebView dismisses. You can use WKNavigationDelegate for detecting WKWebView dismissal.

iPad detect if the user has Dark Mode enabled, without supporting Dark Mode

I'm working on an iPad app. I would like to know if a user has Dark Mode turned on, without actually supporting Dark Mode.
When I have my iPad in Dark Mode, and I ask the O.S for the userInterfaceStyle it always returns 1 (Light Mode).
In my info.plist I've set the User Interface Style to Light. When I remove this value, asking the O.S for the userInterfaceStyle, I get the correct value.
I understand that we should "just support it", however I need evidence that user's are actually using it before I can allocate the resources necessary to complete the task.
When you set the info.plist key, the style will be overridden on the system level and your app will never receive the user's setting.
Alternatively, you could remove the plist key and instead set overrideUserInterfaceStyle on your main window. All subviews of that window will render in light appearance, but you should be able to check for the system style.
By the way, I think a lot of users are using Dark Mode by now, so it's probably worth the effort.

iOS 11+ launch screen - custom named colors in dark mode

Is it possible to use user defined named colors in the launch screen storyboard?
I tried creating test color in xcassets, with appearances {dark, any} and setting it as Launch screen main view background.
I noticed significant differences:
- in RELEASE my custom colors never work
- in DEBUG some colors work, some are ignored, some are changing values (e.x. dark color is correct, but light is biased), and generally it is a mess
I know I need to delete app every time since launch screen is cached, are there any additional constraints? I fail to get any consistent results. Only thing that seems to work is "System background color".
Similar question goes for images.
OK, so short update. It is possible to use named colors and images with dark appearance on the launch screen after all.
It just works like nightmare. Which means that some parts are getting somehow cached (not sure what is happening underneath).
The final result is that to get consistent results, I had not only to delete app from the device, but also restart the phone.
I also noticed, that images with dark appearance are somehow broken - twice bigger or twice smaller, than their "any" appearance counterparts (even when I used exactly same files). That altogether creates unbelievable mess making it hard to test.
This is still a mess in iOS 13. I ended up using the build in dynamic system colours and tinting them with an overlay.

Application NightTime Mode

I have just finished creating my first application and would like to add some additional customisations to it. That is, in my settingsViewController I have added a switch which allows to set the colours to a nighttime mode. My questions is how can I actually get my application to do this.
Thanks in advance for your responses.
You can't expect this thing to be handled by iOS itself because its an app feature that you will have to implement.
You can change background colors to dark and all those UILabel UIButton UITextView dark colored as well to have a feature called dark mode(Dark theme) upon user selection.

What would be the most efficient way of implementing a dark mode in my application?

I have an application that I want to shift to night mode when a button is pressed. What would be the best way of doing that?
I was thinking of creating a class of light vs dark colors to use for certain labels as well as a notification firing when the button is pressed to notify all of the app to adopt the colors. Is there a better way of doing this?
So to set the color, you need to set the properties tintColor or barTintColor or something like this. But I think the real question of yours is, how you can effectifly switch between the two modes.
Your idea with the theme manager class that fires a notification is not that bad, another option would be to observe for theme changes. But firing a notification might be even better.
In this case you only need to listen for the theme-changed-notification inside you UI-subclasses and change their color if the notification fires. And don't forget to remove the NSNotificationCenter-observer in your dealloc :)
If you are building a web application you can use only CSS to implement a light- switch function.
This page looks exactly what you are looking for.
(It also has a CodePen demo.)

Resources