英文:
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)
Also attaching what is 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)
答案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:
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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论