Light and Dark Mode in Swift

Switch between dark and light mode (Swift)

Thanks to your help, I have now managed to do it.

@IBAction func system(_ sender: Any) {
let window = UIApplication.shared.keyWindow
window?.overrideUserInterfaceStyle = .unspecified
}

@IBAction func dunkel(_ sender: Any) {
let window = UIApplication.shared.keyWindow
window?.overrideUserInterfaceStyle = .dark
}

@IBAction func hell(_ sender: Any) {
let window = UIApplication.shared.keyWindow
window?.overrideUserInterfaceStyle = .light
}

The best way to manage colours in dark or light mode

Do what the asset catalog does, without actually using the asset catalog. Define your colors entirely in code as light / dark mode pairs using the UIColor dynamic provider initializer:

https://developer.apple.com/documentation/uikit/uicolor/3238041-init

Now just go ahead and use those colors throughout the app. No need to know whether you're in light or dark mode. No "overhead" required. Your colors will automatically be always correct for the current mode, and will change automatically when the user changes between light and dark modes.

Change between Light and Dark mode for entire app

You could set it on your app's window (it is also a view):

window.overrideUserInterfaceStyle = .dark

Edit:
Add to SceneDelegate file in the willConnectTo func

For example:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
window?.overrideUserInterfaceStyle = .light
}

How to change app theme (Light/Dark) programmatically in swift 5

In your app delegate didFinishLaunchingWithOptions read the preference from shared defaults and do something like this:

UIApplication.shared.keyWindow.overrideUserInterfaceStyle = preference == "dark" ? .dark : .light

In your Settings view controller do the same when the user changes the preference.

Setting the override on your window should take care of all view controllers.

Light and dark mode in swift

let image = UIImage(named: "image") is usually all that is needed, if you setup your asset correctly. Make sure to have "Appearances" set to "Any, Dark", then add your images in the appropriate slots:

Sample Image

Is there a way to change dark to light theme and vice versa just like using a toggle programmitically in swift UI?

WindowGroup {
switch currentThemeRawValue {
case Theme.dark.rawValue:
ContentView().environment(\.colorScheme, .dark)
case Theme.light.rawValue:
ContentView().environment(\.colorScheme, .light)
default:
ContentView().environment(\.colorScheme, .dark)
}
}

The switch statement means that SwiftUI is populating the view with conditional content, so it would rebuild the entire hierarchy if the value changes. You only really want to change the environment value itself, so something like this would probably be better:

WindowGroup {
ContentView()
.environment(\.colorScheme, currentThemeRawValue == "dark" ?? .dark : .light)
}

How to support support dark mode for existing swift app iOS?

if you don't set UIUserInterfaceStyle on info.plist then the App automatically enables dark mode depends on the system. If the system runs with dark mode then the app will start with dark mode.

But to show all the texts or other things you have to use the system color as the background and also for texts. OR you can use a custom color for dark mode or light mode.



Related Topics



Leave a reply



Submit