SwiftUI 如何在 macOS 中为菜单按钮图标着色

问题描述 投票:0回答:2

在 macOS 中,我想为打开菜单的图像着色,无论我做什么,我只是得到文本彩色图标

Menu {
   Button {

   } label: {
      Text("Some item")
   }
} label: {
   Image(systemName: "chevron.down.circle.fill")
      .foregroundColor(.red)
}
.menuStyle(.borderlessButton)
.menuIndicator(.hidden)
.foregroundColor(.blue)
.fixedSize(horizontal: true, vertical: false)

如何像这样给图标上色

macos swiftui menu
2个回答
4
投票

您可以将

foregroundColor
修改器替换为具有两种颜色的
foregroundStyle
,以使用调色板渲染:

Menu {
   Button {

   } label: {
      Text("Some item")
   }
} label: {
   Image(systemName: "chevron.down.circle.fill")
      .foregroundStyle(.white, .green)
}

0
投票

除了@ScottM的答案之外,您还可以使用

.symbolRenderingMode(.palette)
将渲染模式更改为调色板。

这将允许您使用

.foregroundStyle
.foregroundColor
(尽管现已弃用)来更改图标的颜色。

在这种特殊情况下,您仍然需要应用两种颜色来获得您想要的图标样式。

Menu {
   Button {

   } label: {
      Text("Some item")
   }
} label: {
   Image(systemName: "chevron.down.circle.fill")
      .symbolRenderingMode(.palette)
      .foregroundStyle(.white, .green)
}
© www.soinside.com 2019 - 2024. All rights reserved.