Thursday, October 29, 2026
HomeiOS Developmentios - SwiftUI Navigation view - navigate from one youngster view to...

ios – SwiftUI Navigation view – navigate from one youngster view to a different

[ad_1]

I’m utilizing a NavigationView with tag/choice initializer that permits controlling the chosen element view each by way of UI and programmatically, by altering the worth of the binding (mannequin.currentItemId):

@EnvironmentObject var mannequin: Mannequin
var gadgets: [Item]

var physique: some View {
  NavigationView {
    Record {
      ForEach(gadgets) { merchandise in
        NavigationLink(
          tag: merchandise.id,
          choice: $mannequin.currentItemId,
          vacation spot: { ItemView(merchandise: merchandise) },
          label: { ItemPreview(merchandise: merchandise) },
        )
      }
    }
  }
}

It typically works nicely; now I have to navigate from one element view (ItemView) to a different on the button click on:

struct ItemView(): View {
  @EnvironmentObject var mannequin: Mannequin
  var merchandise: Merchandise

  var physique: some View {
    // ...
    Button("open") {
      mannequin.currentItemId = 20 // it's within the listing, however not on the display screen
    }
    // ...
  }
}

and as a substitute of opening the required element view, it opens the listing view.

But when I scroll the listing to the merchandise representing the required element view it could jumps into this element view with none person motion – as quickly as this merchandise seems on the display screen.

Is there a technique to overcome it and open element view with out having the merchandise seen? Or do I’ve to in some way instruct NavigationView to scroll this merchandise into the view?

Thanks!

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments