【发布时间】:2022-10-06 01:53:51
【问题描述】:
使用电视操作系统
我试图在用户长按按钮时出现上下文菜单。
如果我不使用 .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())
}
}
}
有没有人遇到过这个并解决了?谢谢。
-
作为未来参考,这已在 tvOS 16 Beta 4 中得到修复。
-
在发布的 tvOS 16 中再次崩溃。我向苹果提交了反馈。