How to Access an About view via Navigation Bar Button

53 views Asked by At

How do you link an About navigation bar button to an About View? I have seen a number of posts on how to add a button to the navigation bar, but nothing on how to handle a link to the view.

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() -> () {

             ...

     }
}
1

There are 1 answers

0
Asperi On

Here is possible solution - in navigation bar only activate link, but place link inside navigation view

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

...