我正在尝试制作一个锻炼应用程序,当用户完成一组锻炼时,我希望他们能够按下按钮返回主屏幕。
总共有 5 个练习,所以我制作了 5 个练习视图,显示 5 个不同的练习,每个练习视图底部都有一个导航链接,可引导到下一个练习。当用户完成最后一个练习时,我希望底部的按钮将他们带回主屏幕,但当我这样做时,顶部会有一个后退按钮。有没有办法从该视图返回主屏幕而不使用后退按钮?
最后一个练习屏幕的代码:
import SwiftUI
import AVKit
struct ExerciseScreen5View: View {
var countdownTimer = 300
@State var player = AVPlayer()
var exercisePlan: ExercisePlan
var body: some View {
VStack {
Text(exercisePlan.exercise5.title)
.font(.system(size: 35, weight: .medium))
.padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
VideoPlayer(player: exercisePlan.exercise5.video)
.scaledToFit()
.frame(alignment: .center)
.cornerRadius(10)
.padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
Text(exercisePlan.exercise5.steps)
.font(.system(size: 20, weight: .regular))
.padding()
.frame(alignment: .center)
TimerView()
.padding(EdgeInsets(top: 0, leading: 0, bottom: 35, trailing: 0))
NavigationLink(destination: ContentView())
{
Text("Return to Home Screen")
.padding()
.background((Color(red: 184/255, green: 243/255, blue: 255/255)))
.foregroundColor(.black)
.cornerRadius(10)
}
}
}
}
struct ExerciseScreen5View_Previews: PreviewProvider {
static var previews: some View {
ExerciseScreen5View(exercisePlan: ExercisePlan(title: "Exercise Plan 1", details: "Choose this plan for a more basic workout",
exercise: Exercise(title: "Tricep Stretch", duration: 5, steps: "Lift your left elbow straight up while bending your arm. Grab your left elbow with your right hand and pull your left elbow towards your head or slightly behind your head with light pressure. (We recommend doing 10 seconds per rep)", video: AVPlayer(url: Bundle.main.url(forResource: "TricepStretch" , withExtension: "MOV")!)),
exercise2: Exercise(title: "Toe Touches", duration: 5, steps: "Sit with your legs closed and toes pointing up. Keep your knees straight while stretching your arms forward to touch your toes. (We recommend doing 20 seconds per rep)", video: AVPlayer(url: Bundle.main.url(forResource: "ToeTouches" , withExtension: "MOV")!)),
exercise3: Exercise(title: "Arm Circles", duration: 5, steps: "Hold your arms straight out to your sides, then swing them forwards or backwards in circles. Try to keep your shoulders down while doing this exercise. (We recommend doing 20 seconds per rep then changing sides)", video: AVPlayer(url: Bundle.main.url(forResource: "ArmCircles" , withExtension: "MOV")!)),
exercise4: Exercise(title: "Elbow Stretch", duration: 5, steps: "Lift your left arm up while pushing it towards your chest, with your elbow pointing forward. (We recommend doing 10 seconds per rep)", video: AVPlayer(url: Bundle.main.url(forResource: "ElbowStretch" , withExtension: "MOV")!)),
exercise5: Exercise(title: "Calf Raises", duration: 5, steps: "Raise your heels off the floor and return to the starting position, by slowly lowering your heels. (We recommend doing 20 seconds per rep)", video: AVPlayer(url: Bundle.main.url(forResource: "CalfRaises" , withExtension: "MOV")!))
))
}
}
有了
NavigationView
,这不是一件容易完成的任务。那里有解决方案,但是:
NavigationView
现已弃用。您应该使用新的 NavigationStack
Api。
由于您的示例不可重现,我编译了一个更通用的示例,如何实现跨多个级别的导航。
struct ContentView: View{
@State private var navigationPath: NavigationPath = NavigationPath()
var body: some View{
NavigationStack(path: $navigationPath) {
Button{
// go to the first view
// this is implemented as button here, but can also be done by
// a simple NavigtationLink
navigationPath.append("exercise1")
} label: {
Text("up")
}
// here you tell the navigationstack what view to show
.navigationDestination(for: String.self) { name in
switch name{
case "exercise1":
// pass the navigationpath on as binding
ExerciseView1(navigationPath: $navigationPath)
case "exercise2":
ExerciseView2(navigationPath: $navigationPath)
default:
EmptyView()
}
}
}
}
}
struct ExerciseView1: View{
// create a binding to manipulate the navigation stack
@Binding var navigationPath: NavigationPath
var body: some View{
Text("exercise1")
.onTapGesture {
// push the next view to the stack
navigationPath.append("exercise2")
}
}
}
struct ExerciseView2: View{
@Binding var navigationPath: NavigationPath
var body: some View{
Text("exercise2")
Button("Go back"){
// pop back to the root view
navigationPath = NavigationPath()
}
}
}
navigationBarBackButtonHidden()
修饰符来隐藏后退按钮。
dismiss()
函数,该函数是从环境中获得的:
struct MyView: View {
@Environment(\.dismiss) private var dismiss
var body: some View {
Button("Go back") { dismiss() }
}
}
我在这里找到了解决方案:https://sarunw.com/posts/swiftui-dismiss-fullscreencover/
struct MyView: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
Button("Go back") { presentationMode.wrappedValue.dismiss() }
}
}