3

I have recently installed the latest Xcode 11.2.1 and updated iOS to 13.2.3 for iPhone device and 13.x for simulators. I'm trying to implement dark mode into my app, but I only get it to work with the Apple framework stuff, like alertControllers etc. but not with my own viewControllers.

With code I have tried using this just to see if things work:

if #available(iOS 13, *) {
    if traitCollection.userInterfaceStyle == .dark {
        print("DARK MODE")
        cell.backgroundColor = UIColor.black
    }else {
        print("LIGHT MODE")
        cell.backgroundColor = UIColor.white
   }
}

But it's not properly triggered. When dark mode is active, it logs "LIGHT MODE", but it's not vice versa if light mode/normal mode is active; it still triggers "LIGHT MODE" but sometimes seemingly at random it trigger "DARK MODE" instead.

I have also tried using the new system colors on my xib file, but flipping between the different modes changes nothing. I have tried with different colors, like orange, just to see if it shows, and it does, but it's not switched when changing modes.

Finally I have tried adding my own system colors by adding them into the assets folder, setting appearance to "any, light, dark", added alternative colors, then using that color on my xib. This gives me an error: Named colors do not work prior to iOS 11.0. But like I said, I'm using iOS 13, both on simulator and device, so not sure what's up with that.

Just as a note, I have tried switching modes both in system preferences of the device or simulator as well as the new environment override button.

Don't know at all what the deal is. Seems straightforward enough, but can't get it to work at all.

  • I found this trait collection stuff quite unreliable so I did all my code setup in viewDidLayoutSubviews. But if you use interface builder you can set dark mode colors there I think. – andromedainiative Nov 22 '19 at 13:00

2 Answers2

2

That's what I did for my app to support iOS 13 dark mode and devices running ios < 11.0. Let say you have a color in assets with dark and light mode and it's called "White"

if #available(iOS 11.0, *) {
    cell.backgroundColor = UIColor(name: "White")

}else{
    cell.backgroundColor = UIColor.white
}

For devices below IOS 11 it falls on else section and your cell will be white. For devices running 11<= ios <13, it picks the light mode of UIColor(name: "White"). For devices running ios 13.0, it also picks UIColor(name: "White") but according to light/dark mode version of the color defined in assets

0

Above mention error : This gives me an error: Named colors do not work prior to iOS 11.0. But like I said, I'm using iOS 13, both on simulator and device, so not sure what's up with that.

Reason :

There have actual problem is your iOS deployment target below 11.0 so it not work below 11.0.

Solutions:

Change your deployment target it 11.0 above and run your project it's work fine.

Hope it will work.

nimesh surani
  • 177
  • 1
  • 13
  • Ah, thanks, but doesn't that mean that everyone downloading the app will need iOS 11 also? – screenMonkey MonkeyMan Nov 22 '19 at 13:15
  • @screenMonkeyMonkeyMan if you set your deployment target to 11.0, this means your app will support 11.0 and above but nothing lower than 11.0 – Julian Silvestri Nov 22 '19 at 13:20
  • @screenMonkeyMonkeyMan Than, it we would not able to add asset colour and we would not able to achieve dark mode this way. – nimesh surani Nov 22 '19 at 13:31
  • Ok, so asset colors only work if you have iOS 11 as development target. It doesn't revert back to some default color or the "light" color for users below iOS 11? So if you want dark mode for iOS 13 users and "regular mode" for users below iOS 13 you need to use System colors or code then? (System colors do revert to the "light" color for users below 13 right?) – screenMonkey MonkeyMan Nov 22 '19 at 13:41