Search code examples
iosswiftuiviewios-darkmodeuiuserinterfacestyle

How to handle black colored content image for dark mode in my iOS App?


I am giving support for iOS dark mode in my iPad App throughly. The issue is only for dark mode when brand logo image is having black color. Generally, all brand logo are never white colored, so there is no issue for light mode.

Here are the screenshot for both the modes:

Adura brand logo in Light mode

enter image description here

Adura brand logo in Dark mode

enter image description here

How can I accommodate such logos? I got few suggestion to set background view behind the logo with gray color, but again some brand might come having gray colored logo.


Solution

  • Here sample code:

    // Somewhere where you set UIImage for UIImageView
    let imageView = UIImage()
    let image = UIImage(named: "img.png")?.withRenderingMode(.alwaysTemplate)
    imageView.image = image
    imageView.tintColor = .black
    ...
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
            super.traitCollectionDidChange(previousTraitCollection)
            if traitCollection.userInterfaceStyle == .dark {
                imageView.tintColor = .red
            }
            else {
                imageView.tintColor = .black
            }
        }