1

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)
    }
}

2 Answers 2

2

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)
    }
}

}

Sign up to request clarification or add additional context in comments.

2 Comments

This almost works. When I click back and then click the Get Started button again, it does not work a second time.
It is a known XCode simulator bug. In real device, it will work perfectly okay.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.