我可以使用NavigationButton(按入)导航到下一个屏幕,或者与PresentationButton(呈现)一起显示,但是我想在点击Buttton()时进行推送
Button(action: {
// move to next screen
}) {
Text("See More")
}
有办法吗?
答案 0 :(得分:3)
您可以使用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()
}
}
}
}
答案 1 :(得分: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
}
}
}
}
}
}
答案 2 :(得分:0)
答案 3 :(得分:0)
要点击按钮并导航至下一个屏幕,您可以使用如下所示的NavigationLink
NavigationView{
NavigationLink(destination: SecondView()) {
Text("Login")
.padding(.all, 5)
.frame(minWidth: 0, maxWidth: .infinity,maxHeight: 45, alignment: .center)
.foregroundColor(Color.white)
}
}
答案 4 :(得分:-1)
您需要通过在目标视图中将navigationBarBackButtonHidden
设置为true来使用它,如下所示:-
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
}
}