Using tvOS
I am trying to have a contextmenu appear when the user long presses on a button.
If I don't use .buttonStyle() or use one of the built-in buttonStyles, the contextMenu appears.
However, I want to use a custom button style. When I do, the .contextMenu is ignored.
Here is my basic code :
import SwiftUI
struct TestButtonStyle: ButtonStyle {
@Environment(\.isFocused) var focused: Bool
@State private var isFocused: Bool = false
func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding()
.frame(height: 50)
.background(RoundedRectangle(cornerRadius: 20).fill(isFocused ? .red.opacity(0.75) : .clear))
.onChange(of: focused) { hasFocus in
if hasFocus {
isFocused = true
} else {
isFocused = false
}
}
}
}
struct ContentView: View {
var body: some View {
HStack {
Button {
print("Button 1 Pressed")
} label: {
Text("Button 1")
}
.buttonStyle(TestButtonStyle())
.contextMenu {
Button {
//
} label: {
Text("Option 1")
}
Button {
//
} label: {
Text("Option 2")
}
}
Button {
print("Button 2 Pressed")
} label: {
Text("Button 2")
}
.contextMenu {
Button {
//
} label: {
Text("Option 3")
}
Button {
//
} label: {
Text("Option 4")
}
}
.buttonStyle(TestButtonStyle())
}
}
}
Has anyone come across this and solved it? thanks.