【问题标题】:Context Menu Destructive Actions, SwiftUI [duplicate]上下文菜单破坏性操作,SwiftUI [重复]
【发布时间】:2021-01-30 07:34:15
【问题描述】:

我正在尝试在上下文菜单中添加删除操作,但它只是显示为默认的黑色。照片应用程序也使用红色进行删除操作。我在网上的一些空间看到这不是contextMenu 当前提供的功能,但是我也看到它在野外here 中使用了它。有谁知道如何做到这一点?

此外,在 Apple 的 documentation 中查看 contextMenu 时,它表示它们已被弃用,除了 macOS。我觉得很奇怪,他们在引入它一年后就弃用了它。这是否被我应该使用的另一个组件替换?

var body: some View {
        ScrollView {
            LazyVGrid(columns: columns, spacing: 20) {
                ForEach(photos) { photo in
                    Image(uiImage: UIImage(data: photo.imageData!)!)
                        .resizable()
                        .aspectRatio(1, contentMode: .fill)
                        .contextMenu(menuItems: {
                            Button(action: {
                                deletePhoto(selectedPhoto: photo)
                            }) {
                                Label("Remove", systemImage: "trash")
                            }
                        })
                }
            }
            .padding()
            .navigationBarTitle(Text("Albums"))
            .navigationBarItems(trailing:
                Button(action: {
                    self.showingImagePicker = true
                }) {
                    Image(systemName: "plus.circle.fill")
                }
            )
            .sheet(isPresented: $showingImagePicker, onDismiss: loadImage) {
                ImagePicker(image: self.$inputImage)
            }
        }
    }

【问题讨论】:

    标签: ios swift swiftui ios14


    【解决方案1】:

    我玩过不同的东西:fg、bg、口音等,但无济于事。我猜目前“不可能”。

    ps ContextMenu 已弃用,但 .contextMenu 未弃用 你做得对。

    这是未弃用的。另一个不使用 ViewBuilder

    public func contextMenu<MenuItems>(@ViewBuilder menuItems: () -> MenuItems) -> some View where MenuItems : View
    

    【讨论】:

    • 哇,感谢您提供关于 .contextMenu 的信息,但未弃用。它们的命名相当混乱,如果你不知道有两个,就很难区分。
    猜你喜欢
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 2020-02-12
    • 2020-04-18
    • 2014-07-10
    • 2020-08-25
    相关资源
    最近更新 更多