如果我在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)
}
}
如果我使用
Text
或Image
而不是Label
(我知道this),它也不起作用。有没有办法解决这个问题?附注:还有另一个相关的SO问题,但它非常通用且范围更广。
UIKit
创建自己的 it 的示例代码。按照该示例代码,然后将其包装到UIViewControllerRepresentable
或UiViewRepresentable
中,以使其在 SwiftUI 中正常工作。 - lorem ipsum