35

SwiftUI中,您可以使用foregroundColor修饰符更改图标的颜色:
Change the stroke/fill color of SF Symbol icon in SwiftUI?

有没有办法改变颜色UIKit?我查阅了文档并没有找到任何相关的内容。

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

6 回答 6

53

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

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

在此处输入图像描述

在此处输入图像描述

于 2020-04-22T17:32:53.873 回答
42

采用:

let icon = UIImageView(image: iconImage.withRenderingMode(.alwaysTemplate))
icon.tintColor = .red
于 2019-10-06T10:13:50.117 回答
8
let iconImage = UIImage(systemName: "chevron.right",
                                            withConfiguration: UIImage.SymbolConfiguration(pointSize: 16, weight: .regular, scale: .medium))?.withTintColor(.systemGreen)
于 2020-02-12T21:23:23.433 回答
7
muteCall.setImage(UIImage(systemName: "mic.slash")?.withTintColor(.white, renderingMode: .alwaysOriginal), for: .normal)

将渲染设置为始终原始

于 2020-10-22T17:31:53.633 回答
6
let image = UIImage(systemName: "arrow.up.circle.fill")?.withTintColor(.black, renderingMode: .alwaysOriginal)
button.setImage(image, for: .normal)
于 2021-04-23T05:44:23.000 回答
2
@IBOutlet weak var downButton: UIButton!
downButton.setImage(UIImage(systemName: "chevron.down")?.withTintColor(.red, renderingMode: .alwaysOriginal), for: .normal)
于 2021-09-23T18:49:26.263 回答