How to show NavigationLink as a button in SwiftUI

后端 未结 8 2055
说谎
说谎 2021-01-31 14:39

I\'ve read a lot here about navigation in SwiftUI and tried a couple of things, but nothing is working as desired.

Basically, I have a view with a list of workouts and y

相关标签:
8条回答
  • 2021-01-31 15:00

    You don't need to wrap your view inside the NavigationLink to make it trigger the navigation when pressed.

    We can bind a property with our NavigationLink and whenever we change that property our navigation will trigger irrespective of what action is performed. For example-

    struct SwiftUI: View {
        @State private var action: Int? = 0
    
        var body: some View {
        
            NavigationView {
                        VStack {
                            NavigationLink(destination: Text("Destination_1"), tag: 1, selection: $action) {
                                EmptyView()
                            }
                            NavigationLink(destination: Text("Destination_2"), tag: 2, selection: $action) {
                                EmptyView()
                            }
                            
                            Text("Your Custom View 1")
                                .onTapGesture {
                     //perform some tasks if needed before opening Destination view
                                    self.action = 1
                            }
                            Text("Your Custom View 2")
                                .onTapGesture {
                     //perform some tasks if needed before opening Destination view
                                    self.action = 2
                            }
                        }
                    }
            }
    }
    

    Whenever you change the value of your Bindable property (i.e. action) NavigationLink will compare the pre-defined value of its tag with the binded property action, if both are equal navigation takes place.

    Hence you can create your views any way you want and can trigger navigation from any view regardless of any action, just play with the property binded with NavigationLink.

    Thanks, hope it helps.

    0 讨论(0)
  • 2021-01-31 15:00

    You can use NavigationLink as Button with ZStack:

    @State var tag:Int? = nil
    ZStack {
      NavigationLink(destination: MyModal(), tag: 1, selection: $tag) {
        EmptyView()
      }
    
      Button(action: {
        self.tag = 1
      }, label: {
        Text("show view tag 1")
      })
    }
    

    Hope it will help.

    0 讨论(0)
  • 2021-01-31 15:01

    I think that the accurate way to do it is using buttonStyle, for example

    NavigationLink(destination: WorkoutDetail(workout: workout)) {
      WorkoutRow(workout: workout)
    }
    .buttonStyle(ButtonStyle3D(background: Color.yellow))
    
    0 讨论(0)
  • 2021-01-31 15:03

    Use the NavigationLink inside the button's label.

    Button(action: {
        print("Floating Button Click")
    }, label: {
        NavigationLink(destination: AddItemView()) {
             Text("Open View")
         }
    })
    
    0 讨论(0)
  • 2021-01-31 15:04

    You can add the navigation link directly to the button like this:

        @State var tag:Int? = nil
    
        ...
    
        NavigationLink(destination: Text("Full List"), tag: 1, selection: $tag) {
            MyButton(buttonText: "Full list") {
                self.tag = 1
            }
        }
    
    0 讨论(0)
  • 2021-01-31 15:18

    Very similar with Pankaj Bhalala's solution but removed ZStack:

    struct ContentView: View {
        @State var selectedTag: String?
    
        var body: some View {
            Button(action: {
                self.selectedTag = "xx"
            }, label: {
                Image(systemName: "plus")
            })
            .background(
                NavigationLink(
                    destination: Text("XX"),
                    tag: "xx",
                    selection: $selectedTag,
                    label: { EmptyView() }
                )
            )
        }
    }
    

    Wait for a more concise solution without state. How about you?

    0 讨论(0)
提交回复
热议问题