如何从ColorSet显式获得浅色或深色?

问题描述

使用UIColor.init(named: "customColorSet")可以从ColorSet中获得与当前userInterfaceStyle匹配的颜色。

但是,如何从颜色集中获取特定颜色,例如浅色还是深色?

我尝试使用UIColor.init(named: "customColorSet",in: nil,compatibleWith: uitraitcollection(userInterfaceStyle: .light)),但是它似乎没有用,因为仍然返回了与当前userInterfaceStyle匹配的颜色(在我的情况下为深色)。

解决方法

您可以使用UIColor.resolvedColor(with:)

已测试:

let color = UIColor(named: "Color")?.resolvedColor(with: UITraitCollection(userInterfaceStyle: .light))
let rgb = color!.cgColor.components