TVOS,.Contextmenu和ButtonStyle不一起工作

发布于 2025-01-27 18:19:13 字数 1891 浏览 3 评论 0原文

使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

段念尘 2025-02-03 18:19:13

我找到了这个问题的解决方案。

struct MoviesBlankButtonStyle: PrimitiveButtonStyle {
    @Environment(\.isFocused) var focused: Bool
    @State private var isFocused: Bool = false
    
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .compositingGroup()
            .padding(.all, 9)
            .focusable(true, onFocusChange: { focused in
                if focused {
                    isFocused = true
                } else {
                    isFocused = false
                }
            })
            .background(RoundedRectangle(cornerRadius: 20).fill(isFocused ? .red : .clear).opacity(0.7 ))
            .foregroundColor(isFocused ? .white : .white)
            .onTapGesture(perform: configuration.trigger)
    }
}

I have found a solution to this issue.

struct MoviesBlankButtonStyle: PrimitiveButtonStyle {
    @Environment(\.isFocused) var focused: Bool
    @State private var isFocused: Bool = false
    
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .compositingGroup()
            .padding(.all, 9)
            .focusable(true, onFocusChange: { focused in
                if focused {
                    isFocused = true
                } else {
                    isFocused = false
                }
            })
            .background(RoundedRectangle(cornerRadius: 20).fill(isFocused ? .red : .clear).opacity(0.7 ))
            .foregroundColor(isFocused ? .white : .white)
            .onTapGesture(perform: configuration.trigger)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文