I am new to learning in swiftUI, I have to manage following navigation stack in SwiftUI,
Here is my first entry of the app :-
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
Landing()
}
}
}
here is the Landing Page :-
struct Landing: View {
var body: some View {
NavigationStack {
HStack(spacing: 13) {
NavigationLink {
Login()
} label: {
DetezoMainNavigationBtn(title: LandingScreenTexts.logIn)
}
}
}
}
}
here is my Login Page :-
struct Login: View {
@State var isSignInClicked = false
var body: some View {
NavigationStack {
DetezoMainButton(title: LoginScreenTexts.logIn, acttion: {
isSignInClicked = true
})
}
.navigationDestination(isPresented: $isSignInClicked, destination: {
MyZone()
})
}
}
Okay here is my minimal requirement is when I click login from Landing Page then swipe to back gesture and navigation back should work.
But when I click on Login button on login page then swipe to back gesture and navigation back should not work. And navigation Stack should reset and MyZone page should be the new parent of navigation stack
2
Answers
You don’t need a second
NavigationStack
in yourLogin
view. This screen is pushed onto the stack that is in yourLanding
view.Just remove that and it should work fine.
A better way to write this might be to remove the nav stack from your
Landing
screen…If you only want to use NavigationLink to jump to other views, add the .navigationBarBackButtonHidden() modifier to the view that you don’t want navigation back button. This is a solution for your minimal requirement:
From the landing page
From the Login page, add the modifier and hide the back button of the destination view.
Alternatively, you can add the modifier to your MyZone() page
Just so you know, on your Login page, you made a syntax error.
The .navigationDestination should be inside the NavigationStack like this.
You don’t need to add NavigationStack in the Navigation subviews.