Adrian Le Roy Devezin
Adrian Le Roy Devezin

Reputation: 843

Why is NavigationLink not navigation with SwiftUI

I am trying to use a NavigationLink to navigate to the LoginScreen. However clicking up on the button does not do anything. If I call the onTapGesture method then it does get triggered.

struct SplashScreen: View {

    @State
    var shouldGoHome: Bool = false
    @State
    var goToDestination: Bool = false
    @ObservedObject private var viewModel = SplashViewModel()

    var body: some View {
        VStack {
            VColorBackground()
            Text("VOWER")
                .foregroundColor(Color(ColorTheme.brandBlue.value))
                .font(.system(size: 36))
                .tracking(20)
            Text("YOUR TIME DESRVES A\n LOUDER APPLAUSE")
                .tracking(2)
                .multilineTextAlignment(.center)
                .padding(EdgeInsets.init(top: 30, leading: 0, bottom: 0, trailing: 0))
                .font(.system(size: 14))
                .foregroundColor(Color.black)
            if (viewModel.isUserLoggedIn) {
                VowerNavigationButton(text: "Get Started", goToDestination: $goToDestination) {
                    AppView()
                }
                .padding(EdgeInsets.init(top: 70, leading: 0, bottom: 0, trailing: 0))
            } else {
                NavigationLink(destination: LoginScreen()) {
                    VowerButtonStyle(text: "Get Started")
                }
                .padding(EdgeInsets.init(top: 70, leading: 0, bottom: 0, trailing: 0))
            }


            Spacer()

        }
        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
        .edgesIgnoringSafeArea(.all)
    }
}

Upvotes: 1

Views: 393

Answers (2)

Komal Gupta
Komal Gupta

Reputation: 165

For better understanding of Navigation, Please have a look at below code

var body: some View {
    NavigationView{
        VStack(){
            Text("VOWER")
            NavigationLink(destination: detailView){
                VStack(){
                    Text("Sunset").foregroundColor(Color.blue)
                }
            }.navigationBarTitle("Login") //define Title bar for better understanding and ease
        }
    }
}

Similarly in the destination view SwiftUI Class, you can define navigationBarTitle

    var body: some View {
     VStack(){
        Text("Turn Notification On/Off")
     }.navigationBarTitle("Settings")
    }

- It works well on Device, Simulator has this bug of not working for second time.

Upvotes: 1

Mahmud Ahsan
Mahmud Ahsan

Reputation: 2025

You have to wrap the view inside a NavigationView { } otherwise, NavigationLink will not work.

struct SplashScreen: View {

@State
var shouldGoHome: Bool = false
@State
var goToDestination: Bool = false
@ObservedObject private var viewModel = SplashViewModel()

var body: some View {
    NavigationView{ 
        VStack {
            VColorBackground()
            Text("VOWER")
                .foregroundColor(Color(ColorTheme.brandBlue.value))
                .font(.system(size: 36))
                .tracking(20)
            Text("YOUR TIME DESRVES A\n LOUDER APPLAUSE")
                .tracking(2)
                .multilineTextAlignment(.center)
                .padding(EdgeInsets.init(top: 30, leading: 0, bottom: 0, trailing: 0))
                .font(.system(size: 14))
                .foregroundColor(Color.black)
            if (viewModel.isUserLoggedIn) {
                VowerNavigationButton(text: "Get Started", goToDestination: $goToDestination) {
                    AppView()
                }
                .padding(EdgeInsets.init(top: 70, leading: 0, bottom: 0, trailing: 0))
            } else {
                NavigationLink(destination: LoginScreen()) {
                    VowerButtonStyle(text: "Get Started")
                }
                .padding(EdgeInsets.init(top: 70, leading: 0, bottom: 0, trailing: 0))
            }


            Spacer()

        }
        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
        .edgesIgnoringSafeArea(.all)
    }
}

}

Upvotes: 2

Related Questions