0

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")
          }
      }
   }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
ab m
  • 422
  • 3
  • 17
  • 3
    Yes, it is expected. NavigationLink navigates *inside* NavigationView, so if NavigationView smaller then entire screen then you will get what you get. – Asperi Jan 03 '21 at 15:43
  • 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. – ab m Jan 03 '21 at 16:21

1 Answers1

1

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) {
            [...]
        }
    }
}
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223