Search code examples
swiftuicolorswiftui

UIColor dark mode backwards compatibility


I got lots of labels which have the color set to black. With the upcoming of dark mode, they should switch to an other color, since else they will be invisible. With SwiftUI, I thought this goes automatically when setting the Color property to primary.

Is there such thing for UIColor? This label will be invisible in dark mode:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let label = UILabel(frame: .zero)

        label.text = "some text"
        label.textColor = .black

        view.addSubview(label)
        label.translatesAutoresizingMaskIntoConstraints = false

        label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        label.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    }
}

I can make lots and lots and lots of if statements to handle it, but is there an automatic UIColor property which will switch to the appropriate state?


Solution

  • You can look at UIColor.label.