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 you can show a single workout by clicking on a row. This works as expected using NavigationView together with NavigationLink.
Now, I want a button on the detail view to start the workout. This should open a view with a timer. The view should be presented with the same animation as the detail view did and also show the name of the workout in the navigation bar with a back button.
I could implement this with a NavigationLink view on the details page, but the link always appears as a full width row with the arrow on the right side. I'd like this to be a button instead, but the NavigationLink seems to be resistant against styling.
struct WorkoutDetail: View {
var workout: Workout
var body: some View {
VStack {
NavigationLink(destination: TimerView()) {
Text("Starten")
}.navigationBarTitle(Text(workout.title))
}
}
}
struct WorkoutList: View {
var workoutCollection: WorkoutCollection
var body: some View {
NavigationView {
List(workoutCollection.workouts) { workout in
NavigationLink(destination: WorkoutDetail(workout: workout)) {
WorkoutRow(workout: workout)
}
}.navigationBarTitle(Text("Workouts"))
}
}
}
Updated: Here's ascreenshot to illustrate what I mean:
I think that the accurate way to do it is using
buttonStyle
, for exampleUse the NavigationLink inside the button's label.
I've been playing around with this for a few days myself. I think this is what you're looking for.
And create a View you want to show in the NavigationLink
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.
You don't need to wrap your view inside the
NavigationLink
to make it trigger the navigation when pressed.We can just bind a property with our
NavigationLink
and whenever we change that property our navigation will trigger irrespective of what action is performed. For example-Its that simple. Whenever you change the value of your
Bindable
property (i.e. action)NavigationLink
will compare the pre-defined value of itstag
with the binded propertyaction
, 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.
You can use NavigationLink as Button with ZStack:
Hope it will help.