1

I am trying to navigate to a new SwiftUI file that I called HomePageView (Currently just consist of a red background and a text that says Home page.) The code below I tried to integrate with my Button which is 1 of 3 buttons on my initial view which is the ContentView. There are no errors but when I run my code my Login button, it shows the "Login Tapped!" text, but does not take me to the HomePageView. Am I using NavigationLink incorrectly? I know the next problem I will run into is with multiple buttons on one page leading to different destinations, any easy way to solve this, I am trying the tag method?

Note: There is other code in the some View text that are just images and textfields, as well as the two other buttons

@State private var current: Int? = nil

var body: some View {
    NavigationLink(destination: HomePageView(), tag: 1, selection: self.$current) {
        EmptyView()
    }

    Button(action: {
        self.current = 1
        print("Login tapped!")

    }) {
        Text("Login")
            .fontWeight(.bold)
            .foregroundColor(.orange)
            .frame(width: deviceSize.size.width*(275/375), height: deviceSize.size.height*(45/812))
            .cornerRadius(50)
            .overlay(
                Capsule(style: .continuous)
                    .stroke(Color.orange, style: StrokeStyle(lineWidth: 2)))
            .frame(width: deviceSize.size.width, alignment: .center)

    }.offset(y: deviceSize.size.height*(560/812))
}
2
  • 1
    Is there anywhere in view hierarchy above NavigationView? NavigationLink works when being located in NavigationView, so if this is a body of root view, then wrap everything in NavigationView Commented Nov 28, 2019 at 6:14
  • stackoverflow.com/questions/56829974/… Commented Nov 28, 2019 at 10:12

2 Answers 2

2

correct me if my thinking as code below is also about your idea.

var body: some View {
        NavigationView {
            NavigationLink(destination: HomePageView(), tag: 1, selection: self.$current) {
                Button(action: {
                    print("AAAA")
                }) {
                    Text("Login")
                    .fontWeight(.bold)
                    .foregroundColor(.orange)
                    .frame(width: deviceSize.size.width*(275/375), height: deviceSize.size.height*(45/812))
                    .cornerRadius(50)
                    .overlay(
                        Capsule(style: .continuous)
                            .stroke(Color.orange, style: StrokeStyle(lineWidth: 2)))
                    .frame(width: deviceSize.size.width, alignment: .center)
                }


            }
        }
    }

If like as above, that correct what happening to you because it just recognized action of button. Resolve this just remove button, only put text in NavigationLink as below:

var body: some View {
        NavigationView {
            NavigationLink(destination: HomePageView(), tag: 1, selection: self.$current) {
                Text("Login")
                .fontWeight(.bold)
                .foregroundColor(.orange)
                .frame(width: deviceSize.size.width*(275/375), height: deviceSize.size.height*(45/812))
                .cornerRadius(50)
                .overlay(
                    Capsule(style: .continuous)
                        .stroke(Color.orange, style: StrokeStyle(lineWidth: 2)))
                .frame(width: deviceSize.size.width, alignment: .center)
            }
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

your first comment above I agree with it, it seems I was missing the Navigation View and it will not work without it.
0

In my project I used another solution. I don't know if this maybe works for you too:

A created a mother view with

if current == 0 {
    CurrentView()
} else {
    HomePageView()
}

You can also animate it with withAnimation() (see https://developer.apple.com/documentation/swiftui/3279151-withanimation)

If you want to take a look at my implementation you can see it here: https://github.com/tristanratz/ChatApp/blob/master/ClientApp/MainView.swift

1 Comment

This does not push on navigation stack.

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.