Search code examples
swiftuiswiftui-navigationlinklong-pressswiftui-ontapgesture

Restructure NavigationLink to Button for LongPressGesture


I am trying to put an option for .onlongpressgesture for this list of groups. However, it doesn't seem to work and I figure, it will work on Button. I have tried to apply ".onTapGesture" and ".onLongPressGesture", but there is no effect.

Is there a way I can transform the Code below from a NavigationLink to a Button with the same destination when tapped and an additional Menu (called "OptionsmenuView") when long pressed?

The NavigationLink:

 VStack (spacing: 20){
                    ForEach(groups, id:\.self) { Group in
                        NavigationLink(destination: GroupView()) {
                            ZStack (alignment: .bottomLeading) {

                                Image(uiImage: (UIImage(data: Group.groupThumbnail ?? self.image) ?? UIImage(named: "defaultGroupThumbnail"))!)
                                    .resizable(capInsets: EdgeInsets())
                                    .aspectRatio(contentMode: .fill)
                                    .frame(height: 200.0, alignment: .center)
                                    .cornerRadius(22)

                                VStack (alignment: .leading) {
                                    
                                    Text("\(Group.groupTitle ?? "Untitled")")
                                        .font(.title)
                                        .fontWeight(.heavy)
                                        .multilineTextAlignment(.leading)
                                    
                                    Text("Consists of 5 Flowers")
                                }
                                .padding([.leading, .bottom], 18.0)
                                .foregroundColor(.primary)
                            }
                            .listRowBackground(Color.black)
                            
                        }
                    }
                }

The OptionMenuView:

struct OptionsMenuView: View {
    var body: some View {
        
        Menu {
        Button("Cancel", role: .destructive) {
            // Do something
        }
        
        Button {
            // Do something
        } label: {
            Label("Edit", systemImage: "square.and.pencil")
        }
        
        Button {
            // Do something
        } label: {
            Label("Delete", systemImage: "trash")
        }
    } label: {
        Label("Settings", systemImage: "gearshape.fill")
    }
    }
}

I appreciate any form of advice. Thanks in advance.


Solution

  • Couldn't you just use .contextMenu ?

        NavigationView {
            List (0..<10) { i in
                
                NavigationLink {
                    Text("DestionationView")
                } label: {
                    Text("Item \(i)")
                    
                }
                .contextMenu {   // << here
                    Button("Cancel", role: .destructive) {
                        // Do something
                    }
                    
                    Button {
                        // Do something
                    } label: {
                        Label("Edit", systemImage: "square.and.pencil")
                    }
                    
                    Button {
                        // Do something
                    } label: {
                        Label("Delete", systemImage: "trash")
                    }
                }
            }
        }