[英]SwiftUI-Is there a way to go back to the home screen from another view without the back button appearing?
I'm trying to make an exercise app and when a user finishes a set of exercises, I want them to be able to press a button to go back into the home screen.我正在尝试制作一个锻炼应用程序,当用户完成一组锻炼时,我希望他们能够按下按钮 go 回到主屏幕。
There are 5 exercises in total, so I made 5 exercise views that shows the 5 different exercises and each exercise view has a navigation link at the bottom to lead to the next exercise.总共有 5 个练习,所以我制作了 5 个练习视图来显示 5 个不同的练习,每个练习视图的底部都有一个导航链接,可以转到下一个练习。 When a user reaches the last exercise, I want the button at the bottom to bring them back to the home screen but when I did that, there would be a back button at the top.
当用户到达最后一个练习时,我想要底部的按钮将他们带回主屏幕,但当我这样做时,顶部会有一个后退按钮。 Is there any way to go back to the home screen from that view without the back button?
有什么方法可以 go 从该视图返回主屏幕而无需后退按钮?
Code for the last exercise screen:最后一个练习屏幕的代码:
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")!))
))
}
}
You can use the navigationBarBackButtonHidden()
modifier to hide the back button.您可以使用
navigationBarBackButtonHidden()
修饰符来隐藏后退按钮。
To return to the home view, then if you're only one level deep in the navigation stack you can use the system's dismiss()
function, which you get from the environment:要返回主页视图,如果您在导航堆栈中只有一层,则可以使用系统的
dismiss()
function,它是从环境中获取的:
struct MyView: View {
@Environment(\.dismiss) private var dismiss
var body: some View {
Button("Go back") { dismiss() }
}
}
With the NavigationView
this is not an easy task to acomplish.使用
NavigationView
完成这不是一件容易的事。 There are solutions out there but:那里有解决方案,但是:
NavigationView
is deprecated by now. NavigationView
现在已弃用。 You should use the new NavigationStack
Api.您应该使用新的
NavigationStack
Api。
As your example is not reproducible I compiled a more general example how navigation across multiple levels could be implemented.由于您的示例不可重现,因此我编写了一个更通用的示例,说明如何实现跨多个级别的导航。
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()
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.