右键菜单使列表消失。

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

Context menu makes list disappear

问题

我想要一个上下文菜单来复制/粘贴表格数据。

import SwiftUI

struct SwiftUIView: View {
    
    var body: some View {
        List {
            ItemView()
            ItemView()
            ItemView()
        }
        .contextMenu {
            Button(action: {
            }){
                Text("复制")
            }
        }
    }
}

struct ItemView: View {
      
    var body: some View {
        
        HStack{
            Text("列 1")
            Spacer()
            Text("列 2")
            Spacer()
            Text("列 3")
        }
        
    }
}

struct SwiftUIView_Previews2: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
}

然而,在长按列表时,列表会消失。我尝试仅使用 Text("列 1") 来确认 Spacer 是否引起了问题,结果仍然相同。

右键菜单使列表消失。

当使用 Grid 时似乎可以正常工作,但需要 iOS 16。

        Grid {
            GridRow {
                ItemView()
            }
            GridRow {
                ItemView()
            }
        }
        .contextMenu {
            Button(action: {
            }){
                Text("复制")
            }
        }

编辑:

在明亮的主题下,整个屏幕似乎都被选中。

右键菜单使列表消失。

添加 .frame(maxHeight: 200)

右键菜单使列表消失。

右键菜单使列表消失。

有时它可以工作,但我不知道我做了什么不同。

右键菜单使列表消失。

英文:

I want to have a context menu to copy/paste tabular data.

import SwiftUI

struct SwiftUIView: View {
    
    var body: some View {
        List {
            ItemView()
            ItemView()
            ItemView()
        }
        .contextMenu {
            Button(action: {
            }){
                Text("Copy")
            }
        }
    }
}

struct ItemView: View {
      
    var body: some View {
        
        HStack{
            Text("Column 1")
            Spacer()
            Text("Column 2")
            Spacer()
            Text("Column 3")
        }
        
    }
}

struct SwiftUIView_Previews2: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
}

However, the list disappears when long pressing it. I tried using just Text("Column 1") to confirm that the Spacers weren't causing issues and the same thing happened.

右键菜单使列表消失。

It seems to work when using a Grid, but that requires iOS 16.

        Grid {
            GridRow {
                ItemView()
            }
            GridRow {
                ItemView()
            }
        }
        .contextMenu {
            Button(action: {
            }){
                Text("Copy")
            }
        }

Edit:

With light theme, it looks like the entire screen is getting selected.

右键菜单使列表消失。

Adding .frame(maxHeight: 200)

右键菜单使列表消失。

右键菜单使列表消失。

Sometimes it works, but I don't know what I did differently.

右键菜单使列表消失。

答案1

得分: 1

我相信这就是你要找的解决方案:

...
var body: some View {
    List {
        LazyVStack {
            ForEach((0...3), id: \.self) { _ in
                ItemView()
            }
        }
        .contextMenu {
            Button(action: {
            }){
                Text("复制")
            }
        }
    }
}
...

你需要在列表内的视图中调用 contextMenu

英文:

I believe this is the solution you are looking for:

...
var body: some View {
    List {
        LazyVStack {
            ForEach((0...3), id: \.self) { _ in
                ItemView()
            }
        }
        .contextMenu {
            Button(action: {
            }){
                Text("Copy")
            }
        }
    }
}
...

You need to call contextMenu in a view inside the list.

huangapple
  • 本文由 发表于 2023年6月29日 04:54:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576647.html
匿名

发表评论

匿名网友

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

确定