温馨提示:本文翻译自stackoverflow.com,查看原文请点击:swift - SwiftUI changing the color of clear part inside of SF Symbol
image swift swiftui sf-symbols

swift - SwiftUI更改SF Symbol内部透明部分的颜色

发布于 2020-04-08 12:07:48

我正在尝试更改SF Symbol被叫内部的clear(transparent)部分的颜色delete.left.fill到目前为止,我已经尝试如下

Button(action: { return }, label: {
                        Image(systemName: "delete.left.fill")
                            .background(Color.black)
                            //.accentColor(.black)
                            .font(.system(size: self.fontSize*0.75))
                    })
                        .frame(width: self.width, height: self.width)
                        .foregroundColor(.lightGray)
                        //.background(Color.black)

当我如上所述运行代码时,结果就像

符号

首先,x符号内部与背景颜色相同。我要它变黑。

  1. 我尝试将设置为backgroundColorButton然后整个变成Button黑色。
  2. 我想设置accentColorImage黑色。没有改变。
  3. 我想设置backgroundColorImage黑色。结果可以在图像中看到。

问题是,是否有一种方法可以通过编程方式使x里面的东西变成symbol黑色?

查看更多

提问者
Faruk
被浏览
376
nine stones 2020-02-02 01:28

您可以遮罩背景并应用自定义样式:

struct MyButtonStyle: ButtonStyle {
  public func makeBody(configuration: MyButtonStyle.Configuration) -> some View {
    configuration.label
      .compositingGroup()
      .opacity(configuration.isPressed ? 0.5 : 1.0)
  }
}

Button(action: {}) {
  Image(systemName: "delete.left.fill")
    .foregroundColor(.green)
    .background(
      Color.black.mask(Circle())
    )
}.buttonStyle(MyButtonStyle())

该圆圈可能不适合任何用法,但是对于此图像,它可以正常工作:

在此处输入图片说明