ios – SwiftUI How to use onMove without ForEach for new Navigation API?


With new navigation API, the selection is moved from each individual NavigationLink to the whole List.

For example, I had this legacy code:

struct ListView: View {
  @State var selectedString: String? = nil
  
  var body: some View {
    let details = ["foo", "bar", "baz"]
    List {
      ForEach(details, id: \.self) { detail in
        NavigationLink( // deprecated
          detail,
          tag: detail,
          selection: $selectedString,
          destination: { Text("This is detail page for \(detail)") })
      }
      .onMove { from, to in
        // update model
      }
    }

The new API moves the $selection into the List, and it doesn’t require ForEach anymore:

struct ListViewWithNewAPI: View {
  @State var selectedString: String? = nil
  var body: some View {
    List(details, id: \.self, selection: $selectedString) { detail in
      NavigationLink(detail, destination: { Text("This is detail page for \(detail)")})
    }
    .onMove { } // ERROR, `List` does not have onMove function
  }
}

The problem is that, I also have onMove and onDelete in my code, which only applies to ForEach, and not List. How do I handle that?

Note that I cannot use navigationDestination API, because it’s only supported inside a NavigationStack. However, my view is not part of NavigationStack. Instead, it’s using the UIKit’s nav bar. More context can be found here: SwiftUI’s new NavigationStack API doesn’t work well with UIHostingController’s nav bar

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img