'SwiftUI's keyboardShortcut is not working if Button has a buttonStyle
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: [])
Solution 1:[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: [])
}
}
Solution 2:[2]
I had the same Issue, but If I use BorderlessButtonStyle()
instead of PlainButtonStyle
, works for me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | workingdog support Ukraine |
Solution 2 | Jeremy Caney |