无法在 SwiftUI 的菜单内设置按钮标签的颜色
Cannot set color of Button's Label inside Menu in SwiftUI
如果我在 SwiftUI (iOS) 中创建一个 Menu,我无法设置里面按钮的颜色,例如:
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)
}
}
如果我使用 Text
或 Image
(我知道 )而不是 Label
,它也不起作用。
有什么办法吗?
P.S.: 还有一个related SO question,但它非常通用,范围更广。
现在可以在 iOS 15 中通过设置 Button
的角色来实现。 Documentation
示例:
Menu("Actions") {
Button(role: .destructive, action: { }) {
Label("Whatever", systemImage: "pencil")
}
}
结果:
如果我在 SwiftUI (iOS) 中创建一个 Menu,我无法设置里面按钮的颜色,例如:
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)
}
}
如果我使用 Text
或 Image
(我知道 Label
,它也不起作用。
有什么办法吗?
P.S.: 还有一个related SO question,但它非常通用,范围更广。
现在可以在 iOS 15 中通过设置 Button
的角色来实现。 Documentation
示例:
Menu("Actions") {
Button(role: .destructive, action: { }) {
Label("Whatever", systemImage: "pencil")
}
}
结果: