无法在 SwiftUI 中的菜单内设置按钮标签的颜色

nca*_*sas 10 ios swift swiftui swiftui-menu

如果我在 SwiftUI (iOS) 中创建一个菜单,我无法设置里面按钮的颜色,例如:

Menu("Actions") {
    Button(action: { }) {
        Label("Whatever", systemImage: "pencil")
             .background(Color.red)  // does not work
    }
    .background(Color.red)           // does not work either
    .buttonStyle(RedButtonStyle())   // does not work either
}

struct RedButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label.foregroundColor(Color.red)
    }
}
Run Code Online (Sandbox Code Playgroud)

如果Label我使用Text, or Image(我知道这一点)而不是 ,它也不起作用。

有什么办法可以做到吗?

PS:还有另一个相关的SO问题,但它非常通用且范围更广。

Geo*_*e_E 17

现在,在 iOS 15 中可以通过设置 aButton的角色来实现这一点。文档

例子:

Menu("Actions") {
    Button(role: .destructive, action: { }) {
        Label("Whatever", systemImage: "pencil")
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

结果