3

我可以使用 NavigationButton(推送)或使用 PresentationButton(呈现)导航到下一个屏幕,但我想在点击 Button() 时推送

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

有办法吗?

4

4 回答 4

5

你可以使用NavigationLink

注意:请在真机上试用。在模拟器中有时无法正常工作。

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()
            }
        }
    }
}
于 2019-11-09T05:47:45.863 回答
3

如您所见,要显示新视图,添加NavigationLinkwith isActive: $pushViewusing<.hidden()>以隐藏导航“箭头”。

接下来添加Text("See More")tapGesture使文本响应点击。pushView当您单击“查看更多”文本时,变量将更改 (false => true)。

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

内容查看图片

新查看图片

于 2019-10-24T23:06:42.733 回答
0

您可以使用它NavigationLink来实现:

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
                    }
                }
            }
        }
    }
}
于 2019-08-14T14:33:00.973 回答
0

要点击按钮并导航到下一个屏幕,您可以使用 NavigationLink,如下所示

NavigationView{
       NavigationLink(destination: SecondView()) {
             Text("Login")
              .padding(.all, 5)
              .frame(minWidth: 0, maxWidth: .infinity,maxHeight: 45, alignment: .center)
              .foregroundColor(Color.white)
       }
 }
于 2020-04-29T09:43:29.370 回答