SwiftUI How to push to next screen when tapping on Button

依然范特西╮ 提交于 2020-01-10 06:01:17

问题


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

    }
}


来源:https://stackoverflow.com/questions/56829974/swiftui-how-to-push-to-next-screen-when-tapping-on-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!