ios SwiftUI-有没有办法从另一个视图返回到主屏幕,而不显示后退按钮?

b5lpy0ml  于 2022-11-26  发布在  iOS
关注(0)|答案(2)|浏览(208)

我正在尝试做一个锻炼应用程序,当用户完成一组练习时,我希望他们能够按下一个按钮返回主屏幕。
总共有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")!))
                                                      ))
    }
}

gev0vcfq

gev0vcfq1#

使用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()
        }
    }
}
iqih9akk

iqih9akk2#

可以使用navigationBarBackButtonHidden()修饰符隐藏后退按钮。
要返回到主视图,如果您在导航堆栈中只有一层,则可以使用系统的dismiss()函数,该函数可以从环境中获得:

struct MyView: View {
  @Environment(\.dismiss) private var dismiss

  var body: some View {
    Button("Go back") { dismiss() }
  }
}

相关问题