Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context Menu Destructive Actions, SwiftUI [duplicate]

I'm trying to add a delete action in a context menu, however it just gets displayed as the default black color. The Photos app uses a red color for its delete action as well. I've seen in some spaces online that this is not a functionality currently provided of contextMenu, however I have also seen it used in third party apps in the wild here. Does anyone know how to accomplish this?

Also, looking on Apple's documentation for contextMenu it says that they have been deprecated for everything except for macOS. I find it strange that they deprecated it just one year after introducing it. Was this replaced by another component that I should be using?

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)
            }
        }
    }
like image 489
JoshHolme Avatar asked Oct 15 '20 18:10

JoshHolme


1 Answers

I've played with different things: fg, bg, accent etc but to no avail. I'm guessing "not possible" at the moment.

ps ContextMenu is deprecated, but not .contextMenu You're doing it right.

This is the non-deprecated one. The other one doesn't use a ViewBuilder

public func contextMenu<MenuItems>(@ViewBuilder menuItems: () -> MenuItems) -> some View where MenuItems : View
like image 99
Gene De Lisa Avatar answered Sep 28 '22 03:09

Gene De Lisa