TVOS,.Contextmenu和ButtonStyle不一起工作
使用TVOS,
当用户长按按钮上时,我正在尝试出现contextMenu。
如果我不使用.buttonStyle()或使用内置的按钮风格之一,则会出现ContextMenu。
但是,我想使用自定义按钮样式。当我这样做时,.contextmenu被忽略了。
这是我的基本代码:
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())
}
}
}
有人遇到这个问题并解决了吗?谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了这个问题的解决方案。
I have found a solution to this issue.