You need to enable JavaScript to run this app. SwiftUI实现NavigationLink跳转OnboardView及二次启动隐藏引导页
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI实现NavigationLink跳转OnboardView及二次启动隐藏引导页

一、先修复现有代码的基础语法错误

你当前写NavigationLink一直报错的核心原因是语法结构错误:你把CustomShape、ContentView_Previews两个结构体嵌套到了ContentView的实现大括号内部,Swift不允许在结构体的实现块里嵌套定义其他结构体,先调整这部分:删除ContentView末尾多余的大括号,把CustomShape和ContentView_Previews移到ContentView结构体外部即可。


二、正确实现NavigationLink跳转

按以下步骤修改就能完成点击按钮跳转到OnboardView的需求:

  • 给ContentView的根视图套上NavigationStack(iOS16+推荐,如果适配iOS15及更早版本替换成NavigationView即可)
  • 新增跳转控制状态变量,绑定到NavigationLink
  • 替换原来的chevron.right按钮为NavigationLink

修改后的核心代码示例:

import SwiftUI

struct ContentView: View { 
    @State private var animate: Bool = false
    @State private var isJumpToOnboard = false // 新增跳转控制状态
    var body: some View {
        NavigationStack { // 根视图套导航容器
            ZStack{
                // 你原来的图片、VStack等所有代码都保留不动
                
                // 找到你原来写chevron.right按钮的位置,替换成下面的代码
                NavigationLink(isActive: $isJumpToOnboard) {
                    OnboardView()
                } label: {
                    Image(systemName: "chevron.right")
                        .font(.system(size:20, weight: .semibold))
                        .frame(width: 60, height: 60)
                        .foregroundColor(.black)
                        .background(Color.white)
                        .clipShape(Circle())
                        .overlay(
                            ZStack {
                                Circle()
                                    .stroke(Color.black.opacity(0.04),lineWidth: 4)
                                Circle()
                                    .trim(from: 0, to: 0.03)
                                    .stroke(Color.white,lineWidth: 4)
                                    .rotationEffect(.init(degrees: -40))
                            }
                        )
                }
                .padding(-10)
                
                // 你原来的剩余代码都保留不动
            }
            .toolbar(.hidden) // 不需要默认导航栏就加这行隐藏
            .frame(maxWidth: .infinity)
            .edgesIgnoringSafeArea(.all)
            .onAppear(perform: {
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
                    withAnimation(Animation.easeOut(duration: 0.45)){
                        animate.toggle()
                    }
                }
            })
        }
    }
}

// 移到外部的CustomShape和Preview代码
struct CustomShape: Shape {
    var leftCorner: UIRectCorner
    var rightCorner: UIRectCorner
    var radii: CGFloat
    func path(in rect: CGRect) -> Path {
        let path = UIBezierPath(roundedRect: rect, byRoundingCorners: [leftCorner,rightCorner], cornerRadii: CGSize(width: radii, height: radii))
        return Path(path.cgPath)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

三、实现App第二次启动不再展示引导页

你已经用到了@AppStorage持久化存储,直接加一个标记位即可完成需求:

  1. 新增一个持久化标记,记录用户是否已经看过引导页
  2. 用户走完引导页进入首页时,把标记设为已读
  3. App启动时判断标记,已读就直接进入首页,跳过引导流程

代码修改示例:

首先修改OnboardView的逻辑,加已读标记:

struct OnboardView: View {
    @AppStorage("currentPage") var currentPage = 1
    @AppStorage("hasShownOnboard") var hasShownOnboard = false // 新增引导页已读标记
    var body: some View {
        if currentPage > totalPages { 
            Home()
                .onAppear {
                    hasShownOnboard = true // 进入首页时标记引导页已读
                }
        }else{
            WalkthroughScreen()
        }
    }
}

然后修改WalkthroughScreen里最后一步的跳转逻辑(原来测试写的重置为1要改掉):

Button(action: {
    withAnimation(.easeInOut){
        if currentPage < totalPages {
            currentPage += 1
        }else{
            currentPage += 1 // 改成+1触发进入Home的逻辑
        }
    }
}, label: {
    // 按钮原有样式不变
})

最后在App入口文件(比如你的App名.swift文件)加启动判断,直接跳过引导页:

@main
struct YourAppName: App {
    @AppStorage("hasShownOnboard") var hasShownOnboard = false
    var body: some Scene {
        WindowGroup {
            if hasShownOnboard {
                Home() // 已读引导页直接进首页
            } else {
                ContentView() // 未读走原来的启动+引导流程
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者ji hun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 15:54:04