0
votes

I'm trying to navigate to the same view with different data from a navigation link on the current view.

However, I see that the new view when navigated to gets added as a child view below the first view. Like the image shown below. Is this expected?

enter image description here enter image description here

The code looks something like this.

View1 is a

VStack {
   HStack {
      Text("")
   }
   Divider()
   HStack {
      Text("some text")
   }
   NavigationView {
      VStack {
          NavigationLink(destination: View1(data: newData) {
              Text("option")
          }
      }
   }
}
1
Yes, it is expected. NavigationLink navigates inside NavigationView, so if NavigationView smaller then entire screen then you will get what you get.Asperi
Thanks. So is there a way to have navigation links that are only a part of the screen and still have them open views that take up full screen? I suppose I'll have to have the full screen inside a navigation view for that to work? My intent is to have a Text view and a Navigation view driving menu. Upon navigation link click on a menu item, open another view that has some other text in its Text view and new set of menu items in associated navigation view.abhishek mishra

1 Answers

1
votes

Based on your comment, you seem to misunderstand what NavigationView actually does. Think of it as a UINavigationController (because it essentially is one) and NavigationLink as a view with a Touch Up Inside storyboard segue action.

As Asperi said, NavigationView should be the parent view:

var body: some View {
    NavigationView {
        [...]

        NavigationLink(destination: View1(data: newData) {
            [...]
        }
    }
}