简体   繁体   中英

Can't get dark mode to work in my Xcode app

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM