代码之家  ›  专栏  ›  技术社区  ›  NSPratik

如何在iOS应用程序的暗模式下处理黑色内容图像?

  •  0
  • NSPratik  · 技术社区  · 5 年前

    我支持 iOS dark mode 在我的iPad应用程序中。该问题仅适用于品牌标志图像为黑色时的深色模式。一般来说,所有品牌标志都不是白色的,所以光模式没有问题。

    以下是这两种模式的屏幕截图:

    亮模式下的Adura品牌标志

    enter image description here

    黑暗模式下的Adura品牌标志

    enter image description here

    我怎样才能容纳这样的标志?我很少有人建议在标志后面设置灰色背景视图,但有些品牌可能会有灰色标志。

    0 回复  |  直到 5 年前
        1
  •  6
  •   Ku6ep    5 年前

    这里是示例代码:

    // 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
            }
        }
    
        2
  •  6
  •   Quick learner    4 年前

    这对我奏效了

    1. 步骤1-在assets.xassets中创建图像集,如下所示 enter image description here

    2. 步骤2-选择图像,按如下方式选择

    enter image description here

    1. 步骤3-将外观更改为Any、Light、Dark,如下所示 enter image description here

    enter image description here

    1. 为任何亮暗模式添加图像,并在需要时使用此图像

    enter image description here