在SwiftUI的List内的项目中添加contextMenu

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

我创建了一个图片库,其中有很多单元格的列表,每个单元格中都有。有几个UIImages。我添加了contextMenu图像,但是当我长按每个图像时,将调用整个单元格而不是每个图像。谁能帮助我如何将contextMenu添加到列表中的每个项目中

struct PhotoList : View {

var photoLibrary = PhotoLibrary

var body : some View {
    GeometryReader { geometry in
        List(self.photoLibrary, id: \.self) { imageSet in
            HStack (alignment: .center) {
                ForEach(imageSet, id: \.self) { image in
                    Image(uiImage: image)
                        .scaledToFill()
                        .cornerRadius(7)
                        .padding(3)
                        .frame(width: 150, height: 150, alignment: .center)
                        .contextMenu {
                            VStack {
                                Button(action: {}) {
                                    HStack {
                                        Text("Add to Today")
                                        Image("plus.circle")
                                    }
                                }
                            }
                    }

                }
            }
        }
    }
}

}

contextmenu swiftui
1个回答
0
投票

据我所知,上下文菜单必须至少具有两个按钮,但是我认为通过以下方式就足够了:

Image(uiImage: image)
.scaledToFill()
.cornerRadius(7)
.padding(3)
.frame(width: 150, height: 150, alignment: .center)
.contextMenu {
    Button(action: {}) {
        Text("Add to Today")
        Image("plus.circle")
    }
    Button(action: {}) {
        Text("Some Other Button")
        Image("globe")
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.