【问题标题】:How to change SF Symbol icon color in UIKit?如何更改 UIKit 中的 SF Symbol 图标颜色?
【发布时间】:2020-02-03 22:50:32
【问题描述】:

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)

【问题讨论】:

标签: ios colors uikit sf-symbols


【解决方案1】:

用途:

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

【讨论】:

  • 如果你使用 UIButton,像上面那样模板化图像,然后设置按钮的色调颜色。
  • 它在没有第二行的情况下使用它,但在 "icon" 上使用 .image,然后我像往常一样在 SwiftUI 中执行 .foregroundColor(.red) :)
【解决方案2】:
let iconImage = UIImage(systemName: "chevron.right",
                                            withConfiguration: UIImage.SymbolConfiguration(pointSize: 16, weight: .regular, scale: .medium))?.withTintColor(.systemGreen)

【讨论】:

  • 请添加一些注释而不是代码。
【解决方案3】:

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

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

之前

之后

【讨论】:

    【解决方案4】:
    muteCall.setImage(UIImage(systemName: "mic.slash")?.withTintColor(.white, renderingMode: .alwaysOriginal), for: .normal)
    

    将渲染设置为始终原始

    【讨论】:

    【解决方案5】:
    let image = UIImage(systemName: "arrow.up.circle.fill")?.withTintColor(.black, renderingMode: .alwaysOriginal)
    button.setImage(image, for: .normal)
    

    【讨论】:

    【解决方案6】:
    @IBOutlet weak var downButton: UIButton!
    downButton.setImage(UIImage(systemName: "chevron.down")?.withTintColor(.red, renderingMode: .alwaysOriginal), for: .normal)
    

    【讨论】:

    • 您能否添加有关建议代码如何解决问题的详细信息?纯代码答案对提问者和其他人都不是很有帮助。
    • 这个答案被标记为Low Quality 并且可以从解释中受益。以下是How do I write a good answer? 的一些指南。仅代码答案不被视为好的答案,并且可能会被否决和/或删除,因为它们对学习者社区不太有用。这只对你很明显。解释它的作用,以及它与现有答案(如果有的话)有何不同/更好。 From Review
    • 另外,这似乎是从最受好评的答案复制解决方案 - stackoverflow.com/a/61371425/1974224
    猜你喜欢
    • 2019-11-04
    • 1970-01-01
    • 2020-05-17
    • 2018-03-25
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多