在 SwiftUI 的列表中的 VStack 中,上下文菜单无法正常工作

问题描述 投票:0回答:0

首先,我想强调的是,使用

ScrollView
而不是
List
,这个问题不会发生。

我创建了这个测试代码:

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()
}

}

长按显示上下文菜单的结果是整个

VStack
是上下文菜单(而不仅仅是按钮)

同时附上我注释掉 VStack 时的样子:

这是 Apple 的错误吗?有没有办法让它与 VStack 一起工作?在我的真实应用程序中,我有许多带有水平堆栈的部分,显然没有 VStack 或 HStack(懒惰与否)就无法构建真正的应用程序

ios swift list swiftui
© www.soinside.com 2019 - 2024. All rights reserved.