How to show NavigationLink as a button in SwiftUI

后端 未结 8 2056
说谎
说谎 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:19

    I don't know why all these answers are making this so complicated. In SwiftUI 2.0, you just add the button inside the navigation link!

    NavigationLink(destination: TimerView()) {
        Text("Starten")
    }
    

    You can apply SwiftUI styling to the Text object just as you would style any other element.

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

    I've been playing around with this for a few days myself. I think this is what you're looking for.

    struct WorkoutDetail: View {
    var workout: Workout
    
    var body: some View {
        NavigationView {
           VStack {
               NavigationLink(destination: TimerView()) {
                   ButtonView()
               }.navigationBarTitle(Text(workout.title))
            }
        }
    }
    

    And create a View you want to show in the NavigationLink

    struct ButtonView: View {
    var body: some View {
        Text("Starten")
            .frame(width: 200, height: 100, alignment: .center)
            .background(Color.yellow)
            .foregroundColor(Color.red)
    }
    

    Note: Anything above the NavigationView appears to show up on all pages in the Navigation, making the size of the NavigationView Smaller in the links.

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