Context menu在SwiftUI的List中嵌套VStack时无法正常工作。

huangapple go评论70阅读模式
英文:

Context menu is not working properly when in VStack that's in a List in SwiftUI

问题

I'd like to emphasize that with a ScrollView instead of List, this issue doesn't happen.

I have created this test code:

import SwiftUI

struct ContentView: View {
    var body: some View {
        List {
            VStack { // this VStack messes up the context menu
                Button("1", action: {})
                    .contextMenu {
                        Button {
                            print("hi")
                        } label: {
                            Text("test")
                        }
                    }
                Button("2", action: {})
                    .contextMenu {
                        Button {
                            print("hi")
                        } label: {
                            Text("test")
                        }
                    }

                Image(systemName: "globe")
                    .imageScale(.large)
                    .foregroundColor(.accentColor)
                Text("Hello, world!")
            }
        }
        .padding()
    }
}

The result when long-pressing to show the context menu is that the whole VStack is the context menu (instead of just the button).

Also attaching what it looks like when I comment out the VStack.

Is this an Apple bug? Is there a way to get this to work with the VStack? In my real app, I have many sections with horizontal stacks, and obviously, a real app cannot be built without VStack or HStack (lazy or not).

英文:

First, I'd like to emphasize that with a ScrollView instead of List, this issue doesn't happen.

I have created this test code:

import SwiftUI

struct ContentView: View {
    var body: some View {
        List {
            VStack { //this VStack messes up the context menu 
                Button("1", action: {})
                    .contextMenu {
                        Button {
                            print("hi")
                        } label: {
                            Text("test")
                        }
                    }
                Button("2", action: {})
                    .contextMenu {
                        Button {
                            print("hi")
                        } label: {
                            Text("test")
                        }
                    }

            
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Hello, world!")
        }
    }
    .padding()
}

}

The result when long pressing to show the context menu is that the whole VStack is the context menu (instead of just the button)Context menu在SwiftUI的List中嵌套VStack时无法正常工作。

Also attaching what is looks like when I comment out the VStack:

Context menu在SwiftUI的List中嵌套VStack时无法正常工作。

Is this an Apple bug? is there a way to get this to work with the VStack? In my real app, I have many sections with horizontal stacks and obviously a real app cannot be built without VStack or HStack (lazy or not)

答案1

得分: 1

以下是您要翻译的代码部分:

struct ContentView: View {
    var body: some View {
        ScrollView(.horizontal) {
            LazyHStack {
                ForEach(0..<100) { index in
                    Button {
                        print("primary button")
                    } label: {
                        ZStack {
                            Color.red
                            Text("\(index)")
                        }
                        .contextMenu {
                            Button("option 1") {
                                
                            }
                            Button("option 2") {
                                
                            }
                        }
                    }
                    .frame(width: 100)
                }
            }
        }
        .frame(height: 100)
    }
}

希望这对您有所帮助。如果您需要任何其他翻译,请随时告诉我。

英文:

Not sure if you wanted this:

Context menu在SwiftUI的List中嵌套VStack时无法正常工作。

struct ContentView: View {
    var body: some View {
        ScrollView(.horizontal) {
            LazyHStack {
                ForEach(0..&lt;100) { index in
                    Button {
                        print(&quot;primary button&quot;)
                    } label: {
                        ZStack {
                            Color.red
                            Text(&quot;\(index)&quot;)
                        }
                        .contextMenu {
                            Button(&quot;option 1&quot;) {
                                
                            }
                            Button(&quot;option 2&quot;) {
                                
                            }
                        }
                    }
                    .frame(width: 100)
                }
            }
        }
        .frame(height: 100)
    }
}

huangapple
  • 本文由 发表于 2023年3月21日 02:23:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定