How can I switch between dark and light mode in the complete app when I press a button? I'm using Swift 5 and UIKit in Xcode 12
Asked
Active
Viewed 1,870 times
-9
-
2There are lots of articles available on google. Just do some R.&D. – Raja Kishan Jun 30 '21 at 15:35
-
I haven't found an article about this. Do you have a link? – Lasse Stolley Jun 30 '21 at 15:39
-
By adding dark mode. YOu need this https://stackoverflow.com/q/58906624/14733292 – Raja Kishan Jun 30 '21 at 15:47
-
Duplicate of [Is it possible to opt-out of dark mode on iOS 13?](https://stackoverflow.com/q/56537855/2227743) Despite the specific title, all info is there. – Eric Aya Jul 01 '21 at 11:42
2 Answers
4
Try this:
@IBAction func changeMode(_: Any) {
let window = UIApplication.shared.windows[0]
var mode = window.overrideUserInterfaceStyle
mode = mode == .dark ? .light : .dark
}

disepulv
- 262
- 1
- 5
3
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
}

Lasse Stolley
- 29
- 8