可以使用深色和浅色两种颜色来初始化UIColor吗?

时间:2019-11-08 00:39:47

标签: swift uiview uicolor

我在训练应用程序中使用了随机的蜡笔颜色(弹跳的圆圈),但是当我的iPad处于黑暗模式时,这些蜡笔看起来非常淡淡。我可以用两种颜色初始化UIColor吗?也许像这样:

const {PythonShell} = require('python-shell');
const {spawn} = require('child_process')

var cmd = require('node-cmd'); 
var cmd = require('node-cmd'); 
var express = require('express'); 

var options = {
  mode: 'text',
  pythonPath: "C:/Users/path/python.exe",
  pythonOptions: [],
  scriptPath:"C:/Users/path/folder/containing/python/file", 
  args: []
};

PythonShell.run("test.py", options, function (err, results) {
  if(err) throw err;
  console.log('results: %j', results);
});

我当前的应用正在正确创建新的颜色,但是当我从暗模式更改为亮模式时,它们不会自动更改。该应用程序刚刚开始根据UIColor(forLightMode: UIColor, forDarkMode: UIColor) 集生成新的内容。

pastel

2 个答案:

答案 0 :(得分:0)

您需要收听traitCollectionDidChange通知并通过从setNeedsLayout()UIView调用UIViewController或类似方法来重绘UI。有关更多信息,请检查Implementing Dark Mode on iOS〜22分钟

答案 1 :(得分:0)

您可以执行以下操作:

var dynamicColor =  UIColor {(traitCollection: UITraitCollection) -> UIColor in
    if traitCollection.userInterfaceStyle == .dark {
        return .white
    } else {
        return .black
    }
}

来自Ray Wenderlich的电视广播课程(我不隶属于该网站):https://www.raywenderlich.com/3979883-dark-mode-deep-dive