【问题标题】:Changing properties of MenuBarExtra icon更改 MenuBarExtra 图标的属性
【发布时间】:2023-02-15 14:23:45
【问题描述】:

我正在尝试创建一个 MenuBarExtra 允许图标根据 bool 的不同颜色。类似于 This,但图标内的数字变了颜色。

由于 MenuBarExtra 初始值设定项要求字符串而不是视图(我试图以具有属性更改的图像形式传递),因此我无法为图标重新着色

这是我想要的:

MenuBarExtra("label", systemImage: Image(systemName: "circle").foregroundColor(.red))

我收到此错误:

Cannot convert value of type 'some View' to expected argument type 'String'

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

【问题讨论】:

  • 你能分享整个代码吗?

标签: macos swiftui


【解决方案1】:

您不能在 systemImage 中使用 Image(),您只能在其中传递字符串。 如果你想改变文本和图像的颜色,那么下面的代码将起作用:

MenuBarExtra("label", systemImage:"circle").foregroundColor(.red)

【讨论】:

  • 这段代码似乎无法编译。
【解决方案2】:

我花了很长时间才弄清楚如何更改大小。但最后我做到了。

@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!)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 2023-01-16
    • 1970-01-01
    相关资源
    最近更新 更多