I'm trying to build a simple VisionOS app with a single window and i wanna navigate between SwiftUI Views by tapping a custom View embedded inside a NavigationLink as I always did with all SwiftUI apps.
The issue is that nothing happens when I tap on the view, here is the code:
NavigationLink {
TheViewIWantNavigate()
} label: {
MyCustomView()
}
I was able to recognize the tap with this code:
HomeButtonView(text: "Tap Here", image: "myImage")
.onTapGesture {
print("View Tapped")
}
Am I doing something wrong?
Thanks in advance.
To use NavigationLink
in SwiftUI, you should wrap it in a NavigationStack
for proper navigation behavior. Here’s an example of how you can set it up:
NavigationStack {
NavigationLink {
TheViewIWantNavigate()
} label: {
MyCustomView()
}
}
In this code, NavigationStack
provides the necessary context for the navigation, while NavigationLink
specifies the destination (TheViewIWantToNavigateTo()
) and the label (MyCustomView()
) that triggers the navigation.
For more details and best practices on navigation in SwiftUI, you can refer to Apple’s sample code and documentation: “Bringing robust navigation structure to your SwiftUI app”.
Remember that NavigationView
will be deprecated, so transitioning to NavigationStack
or NavigationSplitView
is recommended.