简体   繁体   中英

How to have a NavigationLink with swipeActions in a List in SwiftUI

Consider a List inside of a NavigationView like this:

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                Text("Shovel")
                Text("Bucket")
                Text("Sieve")
            
            }.navigationTitle("Sandbox")
        }
    }
}

I want to add .swipeActions to the trailing edge of the list entries like so:

Text("Shovel")
    .swipeActions(edge: .trailing) {
        Button{} label: { Image(systemName: "trash.fill") }
    }

滑动动作

But once I embed the list entry inside of a NavigationLink , the .swipeActions don't work anymore.

NavigationLink(destination: Text("Shovel")) {
    Text("Shovel")
        .swipeActions(edge: .trailing) {
            Button{} label: { Image(systemName: "trash.fill") }
        }
}

导航链接

Now onto the question:

Why is that and how can I have both of these features?

Thanks for your help.

You should add the SwipeAction to the NavigationLink like so:

NavigationLink(destination: Text("Shovel")) {
    Text("Shovel")
}.swipeActions(edge: .trailing) {
    Button{} label: { Image(systemName: "trash.fill") }
}

This behavior happens because the swipe action is a modifier meant for a List Row . When you had only Text , it was the row.

However, When Embedding your content in any View ( VStack , HStack , NavigationLink ...), that parent becomes the Row .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM