Member-only story
iosAdding dark mode to iOS app
2 min readNov 14, 2019
From iOS 13 apple launched dark theme, If you want to add dark theme in your iOS app you can apply following lines of code on viewDidLoad() like:
if #available(iOS 13.0, *) {
overrideUserInterfaceStyle = .dark
} else {
// Fallback on earlier versions
}
So you can change theme like there is 2 options light or dark theme. But If you are writing above mentioned code it will take always dark theme only on iOS 13 running devices.
overrideUserInterfaceStyle = .light
Or if your device already running on iOS 13, you can change theme like:
you can even check the current set theme like:
if self.traitCollection.userInterfaceStyle == .dark{
print("Dark theme")
}else{
print("Light theme")
}
Let’s see the example:
override func viewDidLoad() {
super.viewDidLoad() if self.traitCollection.userInterfaceStyle == .dark{
self.view.backgroundColor = UIColor.black
}else{
self.view.backgroundColor = UIColor.white
}}
Here is the video for the same: https://youtu.be/_k6YHMFCpas
Results: