I am using a NavigationView with tag/selection initializer that allows controlling the selected detail view both via UI and programmatically, by changing the value of the binding (model.currentItemId):
@EnvironmentObject var model: Model
var items: [Item]
var body: some View {
NavigationView {
List {
ForEach(items) { item in
NavigationLink(
tag: item.id,
selection: $model.currentItemId,
destination: { ItemView(item: item) },
label: { ItemPreview(item: item) },
)
}
}
}
}
It generally works well; now I need to navigate from one detail view (ItemView) to another on the button click:
struct ItemView(): View {
@EnvironmentObject var model: Model
var item: Item
var body: some View {
// ...
Button("open") {
model.currentItemId = 20 // it is in the list, but not on the screen
}
// ...
}
}
and instead of opening the required detail view, it opens the list view.
But if I scroll the list to the item representing the required detail view it would jumps into this detail view without any user action - as soon as this item appears on the screen.
Is there a way to overcome it and open detail view without having the item visible? Or do I have to somehow instruct NavigationView to scroll this item into the view?
Thank you!