[英]SF Symbols Light/Dark Mode
Its possible to apply a custom tint color to SFSymbols image based on Appearance?是否可以根据外观将自定义色调应用于 SFSymbols 图像? By default it goes from black to white and viceversa, but I would like to customize those colors.
默认情况下,它从黑色变为白色,反之亦然,但我想定制那些 colors。 In short, this works as expected, switching from light to dark and viceversa, but I need the control of which tint to apply.
简而言之,这可以按预期工作,从浅色切换到深色,反之亦然,但我需要控制应用哪种色调。
let image = UIImage(systemName: "car")
let imageView = UIImageView(image: image)
private var image = UIImage(systemName: "car")?.withTintColor(.systemRed, renderingMode: .alwaysOriginal)
private var imageView: UIImageView?
private func changeImageTint() {
let tintColor: UIColor = traitCollection.userInterfaceStyle == .dark ? .systemRed : .systemBlue
image = image?.withTintColor(tintColor)
imageView?.image = image
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
changeImageTint()
}
or create a custom color (2 colors) based on userInterfaceStyle and just use that:或基于 userInterfaceStyle 创建自定义颜色(2种颜色)并使用它:
extension UIColor {
static var mySymbolColor: UIColor { UIColor.init { $0.userInterfaceStyle == .dark ? .systemRed : .systemGreen } }
}
button.configuration?.image = UIImage(systemName: "car")?.withTintColor(.mySymbolColor, renderingMode: .alwaysOriginal)
You can create an image instance with different appearances for dark and light mode by adding additional variants to the imageAsset .您可以通过向imageAsset添加其他变体来为深色和浅色模式创建具有不同外观的图像实例。 For example:
例如:
let image = UIImage(systemName: "car")!
.withTintColor(.systemRed, renderingMode: .alwaysOriginal)
let darkImage = UIImage(systemName: "car")!
.withTintColor(.systemBlue, renderingMode: .alwaysOriginal)
image.imageAsset?.register(
darkImage,
with: .init(userInterfaceStyle: .dark)
)
Now image
should be automatically rendered red in light mode and blue in dark mode.现在
image
应该在亮模式下自动渲染为红色,在暗模式下自动渲染为蓝色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.