如何将“关于”导航栏按钮链接到“关于”视图?我看过很多关于如何在导航栏中添加按钮的文章,但是关于如何处理到视图的链接却一无所获。
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: m1View()) {
Text("Menu 1")
}
.padding()
NavigationLink(destination: m2View()) {
Text("Menu 2")
}
.padding()
NavigationLink(destination: m3View()) {
Text("Menu 3")
}
.padding()
}
.font(.title)
.navigationBarTitle("Main View", displayMode: .inline)
.navigationBarItems(trailing: Button(action: {
// TODO: add about link here
}) {
Text("About")
}
)
.onAppear(perform: loadDataViaBankApi)
}
}
func loadDataViaBankApi() -> () {
...
}
}
答案 0 :(得分:0)
这是可能的解决方案-在导航栏中仅激活链接,但将链接放置在导航视图内
struct ContentView: View {
@State private var activateAbout = false
var body: some View {
NavigationView {
VStack {
// ... other content here
}
.font(.title)
.navigationBarTitle("Main View", displayMode: .inline)
.background(
// hide programmatically activated link here !!
NavigationLink(destination: AboutView(), isActive: $activateAbout) { EmptyView() }
)
.navigationBarItems(trailing: Button(action: {
// not add - only activate it here, otherwise it will not work
self.activateAbout = true
}) {
Text("About")
}
)
...