Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the default button color in SwiftUI?

Tags:

macos

swiftui

I'd like to change default MenuButton style. I want to use the default button color instead of 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))

I try to change the default style that looks like enter image description here

to the style like button enter image description here

like image 893
Victor Kushnerov Avatar asked Sep 01 '25 20:09

Victor Kushnerov


1 Answers

I am using it like this:

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

Created Style like this:

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)
    }
}

Use case:

    Button(action: { }, label: {
        Image(systemName: "map")
    })
    .buttonStyle(MainButtonStyle(minWidth: 30, minHeight: 30))
like image 86
Ramis Avatar answered Sep 03 '25 13:09

Ramis