How to switch programmatically to dark mode swift
You can override the style for single views or view controller using the overrideUserInterfaceStyle
property. But since the window is also a view, you can set that on your main window to force it into light or dark mode:
window.overrideUserInterfaceStyle = .dark
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.
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
}
How to change just my app between light and dark mode
If you want to set your app to be only on dark mode, you can simply add
overrideUserInterfaceStyle = .dark
to your viewDidLoad()
, or if you want it to be on the entire app:
- Go to your
Info.plist
file - Add new key:
UIUserInterfaceStyle
with valueDark
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.
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)
}
Implement dark mode switch in SwiftUI App
Single View
To change the color scheme of a single view (Could be the main ContentView
of the app), you can use the following modifier:
.environment(\.colorScheme, .light) // or .dark
or
.preferredColorScheme(.dark)
Also, you can apply it to the ContentView
to make your entire app dark!
Assuming you didn't change the ContentView
name in scene delegate or @main
Entire App (Including the UIKit
parts and The SwiftUI
)
First you need to access the window to change the app colorScheme that called UserInterfaceStyle
in UIKit
.
I used this in SceneDelegate
:
private(set) static var shared: SceneDelegate?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
Self.shared = self
...
}
Then you need to bind an action to the toggle. So you need a model for it.
struct ToggleModel {
var isDark: Bool = true {
didSet {
SceneDelegate.shared?.window!.overrideUserInterfaceStyle = isDark ? .dark : .light
}
}
}
At last, you just need to toggle the switch:
struct ContentView: View {
@State var model = ToggleModel()
var body: some View {
Toggle(isOn: $model.isDark) {
Text("is Dark")
}
}
}
From the UIKit part of the app
Each UIView
has access to the window, So you can use it to set the . overrideUserInterfaceStyle
value to any scheme you need.
myView.window?.overrideUserInterfaceStyle = .dark
Updating programmatically set colors on Mode change (dark mode, light mode) on macOS (objective-c)
As of macOS 11 one should use the performAsCurrentDrawingAppearance: instance method and add anything to apply after an appearance change into the given block.
Related Topics
How to Schedule Mail Using Gmail API
How to Add Action to Uialertview in Swift (iOS 7)
How to Record and Save at 240 Frames Per Second
Why HTML5 Video Doesn't Play in iOS 8 Webapp(Webview)
Fix Cursor Size for Modified Paragraph Spacing in Uitextview
My App Is Getting Crashed on UIdocumentpickerviewcontroller
Customize Google Maps Blue Dot for Current Location
Googlemap API Gives Wrong Coordinates for Direction Between Two Points
Pass Extra Argument for UItapgesturerecognizer with Selector
iOS 11 CPU Throttling and Idle Timer
Get Xcode 5 to Warn About New API Calls
How to Write a Better Data Access Layer with Realm
Uialertview's Textfield Does Not Show Keyboard in iOS8
iOS Framework Does Not Work on Simulator
How to Use Sketch UI Elements Directly into Xcode
How to Compile Aws Customidentityprovider on Xcode 8 Beta 6
iPhone - Memory Leak - Nsdata Datawithcontentsofurl & UIwebview