SwiftUI How to push to next screen when tapping on

2020-02-16 03:01发布

问题:

I can navigate to next screen by using NavigationButton (push) or present with PresentationButton (present) but i want to push when i tap on Buttton()

Button(action: {
// move to next screen
}) {
   Text("See More")
}

is there a way to do it?

回答1:

You can do using NavigationLink

Note: Please try in real device. in simulator sometimes not work properly.

struct MasterView: View {
    @State var selection: Int? = nil

    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: DetailsView(), tag: 1, selection: $selection) {
                    Button("Press me") {
                        self.selection = 1
                    }
                }
            }
        }
    }
}

struct DetailsView: View {
    @Environment(\.presentationMode) var presentation

    var body: some View {
        Group {
            Button("Go Back") {
                self.presentation.wrappedValue.dismiss()
            }
        }
    }
}


回答2:

You can use NavigationLink to implement this:

struct DetailsView: View {
    var body: some View {
        VStack {
            Text("Hello world")
        }
    }
}

struct ContentView: View {
    @State var selection: Int? = nil
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: DetailsView(), tag: 1, selection: $selection) {
                    Button("Press me") {
                        self.selection = 1
                    }
                }
            }
        }
    }
}


回答3:

import SwiftUI

struct ContentView: View {

    @State var pushView = false

    var body: some View {
        NavigationView {

            List {
                HStack{
                    Text("test")
                    Spacer()
                    NavigationLink(destination: NewView(), isActive: $pushView) {
                        Text("")
                    }.hidden()
                        .navigationBarTitle(self.pushView ? "New view" : "default view")
                    Text("See More")
                        .padding(.trailing)
                        .foregroundColor(Color.blue)
                        .onTapGesture {
                            self.pushView.toggle()
                    }
                }
            }
        }
    }
}

struct NewView: View {

    var body: some View {
        Text("New View")
    }
}

ContentView picture

NewView picture



回答4:

You need to use navigationBarBackButtonHidden by setting it true in your destination View like this below :-

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationButton(destination: DetailView()) {
                Text("Click Me")
            }.navigationBarTitle(Text("Header"))             }
    }
}
struct DetailView: View {
    var body: some View {
        Text("Detail View")
        .navigationBarBackButtonHidden(true) //This will hide your back arrow button

    }
}