skip to Main Content

My code:

                Button(action: {
                    AudioServicesPlaySystemSound(1026)
                    isActive.toggle()
                }){
                    HStack{
                        Image(systemName: "trash")
                        Text("delete")
                    }
                }
                .foregroundColor(.red)
                .font(.body)
                .keyboardShortcut("b",modifiers: [])

in this stage keyboardShortcut is working but when I add buttonStyle keyboardShortcut is not working
code with buttonStyle:

                Button(action: {
                    AudioServicesPlaySystemSound(1026)
                    isActive.toggle()
                }){
                    HStack{
                        Image(systemName: "trash")
                        Text("delete")
                    }
                }
                .buttonStyle(PlainButtonStyle())
                .foregroundColor(.red)
                .font(.body)
                .keyboardShortcut("b",modifiers: [])

2

Answers


  1. works well for me on macos 12.beta, xcode 13.beta, target ios 14.7 and MacCatalyst 12. Tested on macOS 12 MacCatalyst app. What system are you using?

    import SwiftUI
    
    @main
    struct TestApp: App {
        var body: some Scene {
            WindowGroup {
                ContentView()
            }
        }
    }
    struct ContentView: View {
        var body: some View {
            Button(action: {
                print("---> button clicked")
            }) {
                HStack{
                    Image(systemName: "trash")
                    Text("delete")
                }
            }
            .buttonStyle(PlainButtonStyle())
            .foregroundColor(.red)
            .font(.body)
            .keyboardShortcut("b",modifiers: [])
        }
    }
    
    Login or Signup to reply.
  2. I had the same Issue, but If I use BorderlessButtonStyle() instead of PlainButtonStyle, works for me.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search