iosswiftios-darkmode

Border color doesn't change when changing themes in iOS 13


I recently developed app which is compatible with Dark mode.

And dark mode also works fine. Btw when I change from dark->light, light->dark mode from device, all colors change as expected except border color.

Let's say border color is black when light mode and white when dark mode and system setting is dark mdoe. When I change system setting to light mode and return to app, all border colors stay white which is supposed be black.

Has anyone ever faced this issue and could you please help me solve this problem? This is serious problem when I want to implement real-time theme update in app.

Thanks.


Solution

  • Thanks to @KurtRevis, I finally managed to solve the problem.

    You need to listen to traitCollectionDidChange. If you want to change borderColor when appearance changes, you need code something like this.

    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
       if #available(iOS 13.0, *) {
           if (traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection)) {
               // ColorUtils.loadCGColorFromAsset returns cgcolor for color name
               layer.borderColor = ColorUtils.loadCGColorFromAsset(colorName: "CellBorderColor")
           }
       }
    }
    

    Hope this helps others