如何在SwiftUI中获取默认按钮颜色?

时间:2019-12-07 08:52:52

标签: swiftui

我想更改默认的MenuButton样式。我想使用默认按钮颜色而不是Color.gray

MenuButton("Actions") {
    Button(action: {
    }, label: { Text("Action 1") })
    Button(action: {
    }, label: { Text("Action 2") })
}
.menuButtonStyle(BorderlessButtonMenuButtonStyle())
.background(RoundedRectangle(cornerRadius: 5).foregroundColor(Color.gray))

我尝试更改看起来像的默认样式 enter image description here

到喜欢按钮的样式 enter image description here

1 个答案:

答案 0 :(得分:0)

我正在这样使用它:

        .foregroundColor(.accentColor)
        .background(Color(UIColor.systemBackground))

这样创建的样式:

struct MainButtonStyle: PrimitiveButtonStyle {
    let minWidth: CGFloat
    let minHeight: CGFloat
    
    func makeBody(configuration: Self.Configuration) -> some View {
        Button(configuration)
            .frame(minWidth: minWidth, minHeight: minHeight)
            .font(.body)
            .foregroundColor(.accentColor)
            .background(Color(UIColor.systemBackground))
            .buttonStyle(BorderlessButtonStyle())
            .cornerRadius(5)
    }
}

用例:

    Button(action: { }, label: {
        Image(systemName: "map")
    })
    .buttonStyle(MainButtonStyle(minWidth: 30, minHeight: 30))