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

时间:2019-10-06 09:46:02

标签: ios sf-symbols

SwiftUI中,您可以使用foregroundColor修饰符更改图标的颜色。因此,我认为应该有一种方法可以更改UIKit中的颜色。我查阅了文档,却没有找到任何相关文件。

现在可以吗?

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

5 个答案:

答案 0 :(得分:19)

使用以下代码更改SFSymbols图标的颜色

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

之前

enter image description here

之后

enter image description here

答案 1 :(得分:4)

使用:

let icon = UIImageView(image: iconImage.withRenderingMode(.alwaysTemplate))
icon.tintColor = .red

答案 2 :(得分:3)

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

答案 3 :(得分:2)

muteCall.setImage(UIImage(systemName: "mic.slash")?.withTintColor(.white, renderingMode: .alwaysOriginal), for: .normal)

将渲染设置为始终原始

答案 4 :(得分:1)

let image = UIImage(systemName: "arrow.up.circle.fill")?.withTintColor(.black, renderingMode: .alwaysOriginal)
button.setImage(image, for: .normal)