skip to Main Content

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


  1. You don’t need a second NavigationStack in your Login view. This screen is pushed onto the stack that is in your Landing 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…

    import SwiftUI
    
    @main
    struct MyApp: App {
      var body: some Scene {
    
        @State var isLoggedIn = false
    
        WindowGroup {
          if isLoggedIn {
            NavigationStack {
              MainScreen()
            }
          } else {
            NavigationStack {
              Landing()
            }
          }
        }
      }
    }
    
    struct Landing: View {
        var body: some View {
            HStack(spacing: 13) {
                NavigationLink {
                    Login()
                } label: {
                    DetezoMainNavigationBtn(title: LandingScreenTexts.logIn)
                }
            }
        }
    }
    
    struct Login: View {
    // etc
    }
    
    
    Login or Signup to reply.
  2. 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

    struct Landing: View {
        var body: some View {
            NavigationStack {
                HStack(spacing: 13) {
                    NavigationLink {
                        Login()
                    } label: {
                        Text("Landing Screen Log In")
                    }
                }
            }
        }
    }
    

    From the Login page, add the modifier and hide the back button of the destination view.

    struct Login: View {
        @State var isSignInClicked = false
        var body: some View {
            Button("Log in") {
                isSignInClicked = true
            }
            .navigationDestination(isPresented: $isSignInClicked, destination: {MyZone().navigationBarBackButtonHidden()})
        }
    }
    

    Alternatively, you can add the modifier to your MyZone() page

    struct MyZone: View {
        var body: some View {
            VStack {
                Text("My Zone")
            }
            .navigationBarBackButtonHidden()
        }
    }
    

    Just so you know, on your Login page, you made a syntax error.
    The .navigationDestination should be inside the NavigationStack like this.

        NavigationStack {
            Button() 
                .navigationDestination()
        }
    

    You don’t need to add NavigationStack in the Navigation subviews.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search