swiftuitoolbarsf-symbols

How to apply symbolRenderingMode to a single toolbar button?


Consider the following code:

struct ContentView: View {
    var body: some View {
        NavigationStack {
            Text("Text")
                .navigationTitle("Title")
                .toolbar {
                    Button("Button", systemImage: "line.3.horizontal.decrease.circle.fill", action: {})
                        .symbolRenderingMode(.hierarchical)
                        .foregroundStyle(.blue)
                }
        }
    }
}

I would expect the following:
Expected result, with correct symbol rendering

But I get this instead:
Actual result, with a monochrome button

If I modify the code by adding a second button,

.toolbar {
    HStack {
        Button("Button", systemImage: "line.3.horizontal.decrease.circle.fill", action: {})
            .symbolRenderingMode(.hierarchical)

        Button("Button", systemImage: "line.3.horizontal.decrease.circle.fill", action: {})
            .symbolRenderingMode(.hierarchical)
            .foregroundStyle(.blue)
    }
}

I get the expected:
Two buttons with correct colour and symbol rendering

However, this is of course far from ideal. I tried adding an "empty" Button("", action: {}) instead to the HStack and it works, but it's really just a hack.

Any ideas ?


Solution

  • When creating the button's image yourself, the color is respected:

    .toolbar {
      Button(action: {}) {
        Image(systemName: "line.3.horizontal.decrease.circle.fill")
          .symbolRenderingMode(.hierarchical)
          .foregroundStyle(.blue)
    }
    

    Looks like a bug in Button.