简体   繁体   English

如何更改 UIKit 中 SF Symbols 的图标颜色?

[英]How to change SF Symbols' icon color in UIKit?

In SwiftUI , you can change the icon's color using foregroundColor modifier.SwiftUI中,您可以使用foregroundColor修饰符更改图标的颜色。 So I think there should be a way to change the color in UIKit .所以我认为应该有一种方法可以改变UIKit的颜色。 I looked up the documentation and didn't find anything related to it.我查阅了文档并没有找到任何相关的内容。

Is it possible right now?现在有可能吗?

let iconImage = UIImage(systemName: "chevron.right", 
    withConfiguration: UIImage.SymbolConfiguration(pointSize: 16, weight: .regular, scale: .medium))

Use below code for changing SFSymbols icons color使用以下代码更改 SFSymbols 图标颜色

let imageIcon = UIImage(systemName: "heart.fill")?.withTintColor(.red, renderingMode: .alwaysOriginal)
    imageView.image = imageIcon

Before

在此处输入图像描述

After

在此处输入图像描述

Use:利用:

let icon = UIImageView(image: iconImage.withRenderingMode(.alwaysTemplate))
icon.tintColor = .red
muteCall.setImage(UIImage(systemName: "mic.slash")?.withTintColor(.white, renderingMode: .alwaysOriginal), for: .normal)

Set rendering to always original将渲染设置为始终原始

let iconImage = UIImage(systemName: "chevron.right",
                                            withConfiguration: UIImage.SymbolConfiguration(pointSize: 16, weight: .regular, scale: .medium))?.withTintColor(.systemGreen)
let image = UIImage(systemName: "arrow.up.circle.fill")?.withTintColor(.black, renderingMode: .alwaysOriginal)
button.setImage(image, for: .normal)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM