更改 MenuBarExtra 图标的属性

Bai*_*ley 2 macos swiftui

我正在尝试创建一个 MenuBarExtra ,允许图标根据布尔值具有不同的颜色。像这样的东西,但颜色发生变化,而不是图标内的数字。

我在重新着色图标时遇到麻烦,因为 MenuBarExtra 初始化程序要求提供字符串而不是视图(我试图以具有属性更改的图像的形式传递)

这就是我想要的:

MenuBarExtra("label", systemImage: Image(systemName: "circle").foregroundColor(.red))
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Cannot convert value of type 'some View' to expected argument type 'String'
Run Code Online (Sandbox Code Playgroud)

我可以传递“circle”作为 systemImage 的参数,但不能传递图像。当需要字符串时,有什么方法可以更改 systemImage 属性吗?

小智 9

我花了很长时间才弄清楚如何改变尺寸。但最终我成功了。

@State var isInserted = true
@State var colorIndex = 0
let colors: [NSColor] = [.red, .blue, .orange]
MenuBarExtra(isInserted: $isInserted) {
    Button("Shift") {
        colorIndex = (colorIndex+1) % colors.count
    }
} label: {
    let configuration = NSImage.SymbolConfiguration(pointSize: 16, weight: .light)
                .applying(.init(paletteColors: [colors[colorIndex]]))
    let image = NSImage(systemSymbolName: "camera", accessibilityDescription: nil)
    let updateImage = image?.withSymbolConfiguration(configuration)
    Image(nsImage: updateImage!)
}
Run Code Online (Sandbox Code Playgroud)