I am a working on an app that has a list of sports, i built a reusable view for the name and image of sports to be used to list each sport. Then i made a view for each sport to be opened when a user taps a sport image from the list of sports, i created a variable for image, sportname and link, i dont know how to link a variable with NavigationLink. I am lost here, any help will be really appreciated. Below is my code.
struct SportTypeInsetView: View {
var buttonImage1: String
var buttonName1: String
var buttonLink1: String
var body: some View {
VStack {
NavigationView {
VStack {
Button (action: {
}) {
Image(buttonImage1)
.resizable()
.clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous))
}
.frame(width: 50, height: 30)
Text(buttonName1)
.font(.footnote)
.foregroundColor(.primary)
.multilineTextAlignment(.leading)
.lineLimit(1)
}
}
}
}
}
SportTypeInsetView(buttonImage1: "Yoga-icon", buttonName1: "Yoga",buttonLink1: )
How do i connect it to YogaView() when the button is tapped?
Thanks.
You can use NavigationLink with button Style
DefaultButtonStyle
.NavigationLink has a label parameter, which is basically a SwiftUI view object, you can simply pass a
Text
object so that it looks like button or pass bothImage
andText
, it's up to you, end result will be button styleNavigationLink
.Providing a button style helps to give you a similar feel like button.
In case you want to customize the NavigationLink Label View, you can do something like this.
To achieve a Navigation link which is dynamic in behavior,
SportTypeInsetView
can have a generic TypeTargetView
which is constrained byView
protocol.With that being said you would not need
buttonLink1
as thetargetView
itself is dynamic.