【发布时间】:2020-10-04 09:23:02
【问题描述】:
我正在使用扩展和结构中的 UIColor 和 CGColor 进行练习。一切正常,但我意识到如果将来我将添加“主题”功能,如果我从红色更改为蓝色,我如何在更改主题时更新所有 UIColor 和 CGColor?
例如,当我按下按钮将主题更改为蓝色时,它如何更新所有 UIColor / CGColor?
这是我的代码:
extension UIColor {
public class func dynamicColor(light: UIColor, dark: UIColor) -> UIColor {
return UIColor {
switch $0.userInterfaceStyle {
case .dark: return dark
default: return light
}
}
}
}
var themePick = "Red"
struct AiooColor {
static var tableviewerHeaderTitle: UIColor {
switch themePick {
case "Red": return UIColor.dynamicColor(light: UIColor.systemRed, dark: UIColor.systemRed)
case "Blue": return UIColor.dynamicColor(light: UIColor.systemBlue, dark: UIColor.systemBlue)
// DEFAULT WILL SET TO RED
default: return UIColor.dynamicColor(light: UIColor.systemRed, dark: UIColor.systemRed)
}
}
static var tableviewerHeaderBackground: UIColor {
switch themePick {
case "Red": return UIColor.dynamicColor(light: UIColor(red: 255/255, green: 204/255, blue: 204/255, alpha: 1), dark: UIColor(red: 30/255, green: 0/255, blue: 0/255, alpha: 1))
case "Blue": return UIColor.dynamicColor(light: UIColor(red: 204/255, green: 204/255, blue: 255/255, alpha: 1), dark: UIColor(red: 0/255, green: 0/255, blue: 30/255, alpha: 1))
// DEFAULT WILL SET TO RED
default: return UIColor.dynamicColor(light: UIColor(red: 20/255, green: 0/255, blue: 0/255, alpha: 1), dark: UIColor(red: 20/255, green: 0/255, blue: 0/255, alpha: 1))
}
}
}
【问题讨论】:
标签: ios swift static uicolor cgcolor